Ejemplo n.º 1
0
        private async Task LogCommandAsync(Optional <CommandInfo> command, ICommandContext context)
        {
            var guild       = context.Guild == null ? "NoGuild" : $"{context.Guild.Name} ({context.Guild.Id})";
            var channel     = context.Channel == null ? "NoChannel" : $"#{context.Channel.Name} ({context.Channel.Id})";
            var args        = $"{guild}, {channel}, @{context.User}, ({context.Message.Content})";
            var commandName = command.IsSpecified ? $"{command.Value.Module.Group} {command.Value.Name}".Trim() : "Unknown command";

            Logger.LogInformation("Executed {0}.\t{1}", commandName, args);

            if (command.IsSpecified)
            {
                InternalStatistics.IncrementCommand(commandName);
            }

            if (context.Guild != null && command.IsSpecified)
            {
                var cmd = command.Value;

                using var scope = Services.CreateScope();
                var grillBotRepository = scope.ServiceProvider.GetService <IGrillBotRepository>();

                var config = await grillBotRepository.ConfigRepository.FindConfigAsync(context.Guild.Id, cmd.Module.Group, cmd.Name, false);

                if (config == null)
                {
                    config = MethodsConfig.Create(context.Guild, cmd.Module.Group, cmd.Name, false, new JObject());
                    grillBotRepository.Add(config);
                }

                config.UsedCount++;
                await grillBotRepository.CommitAsync();
            }
        }
Ejemplo n.º 2
0
        public async Task ImportConfigurationAsync()
        {
            var attachment = Context.Message.Attachments.FirstOrDefault(o => Path.GetExtension(o.Filename) == ".json");

            if (attachment == null)
            {
                await ReplyAsync("Nebyl nalezen žádný JSON soubor.");

                return;
            }

            var bytes = await attachment.DownloadFileAsync();

            var jsonData     = Encoding.UTF8.GetString(bytes);
            var importedData = JsonConvert.DeserializeObject <List <MethodsConfig> >(jsonData);

            var state = Context.Channel.EnterTypingState();

            try
            {
                using var service = GetService <IGrillBotRepository>();

                foreach (var method in importedData)
                {
                    if (await service.Service.ConfigRepository.ConfigExistsAsync(method.GuildIDSnowflake, method.Group, method.Command))
                    {
                        await ReplyAsync($"> Metoda `{method}` již existuje. **Ignoruji!**");

                        continue;
                    }

                    var entity = MethodsConfig.Create(method.GuildIDSnowflake, method.Group, method.Command, method.OnlyAdmins, method.Config);

                    foreach (var perm in method.Permissions)
                    {
                        entity.Permissions.Add(new MethodPerm()
                        {
                            AllowType = perm.AllowType,
                            DiscordID = perm.DiscordID,
                            PermType  = perm.PermType
                        });
                    }

                    await service.Service.AddAsync(entity);
                    await ReplyAsync($"> Import metody `{method}` připraven.");
                }

                await service.Service.CommitAsync();
            }
            finally
            {
                state.Dispose();
            }

            await ReplyAsync("Uložení bylo úspěšné. Data importována.");

            await Context.Message.AddReactionAsync(new Emoji("✅"));
        }
Ejemplo n.º 3
0
        public MethodsConfig AddConfig(SocketGuild guild, string group, string command, bool onlyAdmins, JObject json)
        {
            CorrectValue(ref group);
            CorrectValue(ref command);

            var entity = MethodsConfig.Create(guild, group, command, onlyAdmins, json);

            Context.MethodsConfig.Add(entity);
            Context.SaveChanges();

            return(entity);
        }
Ejemplo n.º 4
0
        public async Task AddMethodAsync(GroupCommandMatch command, string onlyAdmins, [Remainder] JObject configJson)
        {
            if (command.MethodID != null)
            {
                await ReplyAsync("Tato konfigurace již existuje.");

                return;
            }

            var adminsOnly = Convert.ToBoolean(onlyAdmins);
            var config     = MethodsConfig.Create(Context.Guild, command.Group, command.Command, adminsOnly, configJson);

            using var service = GetService <IGrillBotRepository>();
            await service.Service.AddAsync(config);

            await service.Service.CommitAsync();

            await ReplyAsync($"Konfigurační záznam `{command},OA:{adminsOnly},ID:{config.ID}` byl úspěšně přidán.");
        }
Ejemplo n.º 5
0
        public void IncrementUsageCounter(IGuild guild, string group, string command)
        {
            CorrectValue(ref group);
            CorrectValue(ref command);

            var guildID = guild?.Id.ToString();

            var entity = GetBaseQuery(false)
                         .FirstOrDefault(o => o.GuildID == guildID && o.Group == group && o.Command == command);

            if (entity == null)
            {
                entity = MethodsConfig.Create(guild, group, command, false, null);
                Context.MethodsConfig.Add(entity);
            }

            entity.UsedCount++;
            Context.SaveChanges();
        }