Пример #1
0
        public async Task <IActionResult> UpdateCustomCommandsPlugin([FromRoute] string guildId,
                                                                     [FromBody] CustomCommandsPluginDto customCommandsPluginDto)
        {
            var result = await _pluginService.UpdateCustomCommandsPluginAsync(guildId, customCommandsPluginDto,
                                                                              await _userManager.GetUserAsync(User));

            if (!result)
            {
                return(BadRequest());
            }

            return(Ok());
        }
Пример #2
0
        public async Task <bool> UpdateCustomCommandsPluginAsync(string guildId, CustomCommandsPluginDto customCommandPluginDto, User user)
        {
            try
            {
                var guild = await _guildService.GetByGuildIdAsync(guildId);

                var discordGuild = await _botService.GetGuildAsync(guildId);

                var member = await discordGuild.GetMemberAsync(ulong.Parse(user.UserId));

                //var initiator =
                //    await _userService.GetByUsernameAndDiscriminatorAsync(user.UserName, user.Discriminator);

                var hasPermission = await CheckIfHasPermissionAsync(member, discordGuild, guild);

                if (!hasPermission)
                {
                    return(false);
                }

                // TODO: For some reason, this is needed or the deleted commands will not be removed
                Console.WriteLine("Commands BEFORE modification: " + guild.CustomCommandPlugin.Commands);

                guild.CustomCommandPlugin.Commands = customCommandPluginDto.Commands;

                guild.CustomCommandPlugin.Commands = new List <Command>();
                foreach (var command in customCommandPluginDto.Commands)
                {
                    if (command.Id != null)
                    {
                        command.Id = new Guid();
                    }

                    guild.CustomCommandPlugin.Commands.Add(command);
                }

                // TODO: For some reason, this is needed or the deleted commands will not be removed
                Console.WriteLine("Commands BEFORE modification: " + guild.CustomCommandPlugin.AdvancedCommands);

                guild.CustomCommandPlugin.AdvancedCommands = customCommandPluginDto.AdvancedCommands;

                guild.CustomCommandPlugin.AdvancedCommands = new List <AdvancedCommand>();
                foreach (var command in customCommandPluginDto.AdvancedCommands)
                {
                    if (command.Id != null)
                    {
                        command.Id = new Guid();
                    }

                    guild.CustomCommandPlugin.AdvancedCommands.Add(command);
                }

                if (guild.CustomCommandPlugin.IsEnabled != customCommandPluginDto.IsEnabled)
                {
                    guild.CustomCommandPlugin.IsEnabled = customCommandPluginDto.IsEnabled;
                }

                await _logService.AddAsync(
                    $"CHANGED_CUSTOM_COMMANDS_PLUGIN_SETTINGS",
                    ActionType.Update,
                    user,
                    guildId
                    );

                await _pluginRepository.SaveAllAsync();

                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }