Пример #1
0
        public async Task GetJsonConfig(GroupCommandMatch method)
        {
            if (await CheckMissingMethodID(method))
            {
                return;
            }

            using var service = GetService <IGrillBotRepository>();
            var config = await service.Service.ConfigRepository.FindConfigAsync(Context.Guild.Id, method.Group, method.Command, false);

            if (config == null)
            {
                await ReplyAsync("Požadovaná metoda neexistuje.");

                return;
            }

            if (config.ConfigData.Length >= DiscordConfig.MaxMessageSize - 11)
            {
                using var stream = new MemoryStream(Encoding.UTF8.GetBytes(config.ConfigData));
                await Context.Channel.SendFileAsync(stream, $"{method.Group}_{method.Command}.json");

                return;
            }

            await ReplyAsync($"```json\n{config.ConfigData}```").ConfigureAwait(false);
        }
Пример #2
0
        public async Task RemovePermissionAsync(GroupCommandMatch method, int permID)
        {
            if (await CheckMissingMethodID(method))
            {
                return;
            }

            using var service = GetService <IGrillBotRepository>();

            var config = await service.Service.ConfigRepository.FindConfigAsync(Context.Guild.Id, method.Group, method.Command, true);

            if (config == null)
            {
                await ReplyAsync("Požadovaná metoda neexistuje");

                return;
            }

            var permission = config.Permissions.FirstOrDefault(o => o.PermID == permID);

            if (permission == null)
            {
                await ReplyAsync("Požadované oprávnění neexistuje.");

                return;
            }

            config.Permissions.Remove(permission);
            await service.Service.CommitAsync();

            await ReplyAsync("Oprávnění bylo odebráno");
        }
Пример #3
0
        private async Task <bool> CheckMissingMethodID(GroupCommandMatch command)
        {
            if (command.MethodID == null)
            {
                await ReplyAsync($"Hledaná konfigurace pro metodu `{command}` nebyla nalezena.");

                return(true);
            }

            return(false);
        }
Пример #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.");
        }
Пример #5
0
        public async Task ListPermissionsAsync(GroupCommandMatch method)
        {
            if (await CheckMissingMethodID(method))
            {
                return;
            }
            await Context.Guild.SyncGuildAsync();

            using var service = GetService <IGrillBotRepository>();
            var config = await service.Service.ConfigRepository.FindConfigAsync(Context.Guild.Id, method.Group, method.Command, true);

            await ReplyAsync($"ID: `{config.ID}`\nSkupina/Příkaz: `{config.Group}/{config.Command}`\nOnlyAdmins: `{config.OnlyAdmins}`");

            var rowsData = config.Permissions.Select(o =>
            {
                switch (o.PermType)
                {
                case PermType.Role:
                    var role = Context.Guild.GetRole(o.DiscordIDSnowflake);
                    return($"{o.PermID}\t{role?.Name ?? "Neznámá role"} ({role?.Id ?? o.DiscordIDSnowflake})\t{o.PermType}\t{o.AllowType}");

                case PermType.User:
                    var user = Context.Guild.GetUserFromGuildAsync(o.DiscordID).Result;
                    return($"{o.PermID}\t{user.GetFullName()}\t{o.PermType}\t{o.AllowType}");

                case PermType.Everyone:
                    return($"{o.PermID}\tEveryone\t-\t{o.AllowType}");
                }

                return(null);
            }).Where(o => o != null).ToList();

            if (rowsData.Count > 0)
            {
                rowsData.Insert(0, "ID\tUživatel/Role\tTyp práva\tTyp povolení");
                await ReplyAsync($"```{string.Join("\n", rowsData)}```");
            }
            else
            {
                await ReplyAsync("Tato metoda nemá nastaveny žádné oprávnění.");
            }
        }
Пример #6
0
        public async Task SwitchOnlyAdminsAsync(GroupCommandMatch method, string onlyAdmins)
        {
            if (await CheckMissingMethodID(method))
            {
                return;
            }

            using var service = GetService <IGrillBotRepository>();
            var config = await service.Service.ConfigRepository.FindConfigAsync(Context.Guild.Id, method.Group, method.Command, false);

            if (config == null)
            {
                await ReplyAsync("Požadovaná metoda neexistuje.");

                return;
            }

            config.OnlyAdmins = Convert.ToBoolean(onlyAdmins);
            await service.Service.CommitAsync();

            await ReplyAsync("Příkaz byl úspěšně aktualizován.").ConfigureAwait(false);
        }
Пример #7
0
        public async Task RemoveMethodAsync(GroupCommandMatch method)
        {
            if (await CheckMissingMethodID(method))
            {
                return;
            }

            using var service = GetService <IGrillBotRepository>();
            var config = await service.Service.ConfigRepository.FindConfigAsync(Context.Guild.Id, method.Group, method.Command, true);

            if (config == null)
            {
                await ReplyAsync("Požadovaná metoda neexistuje.");

                return;
            }

            service.Service.Remove(config);
            await service.Service.CommitAsync();

            await ReplyAsync("Metoda byla odebrána.");
        }
Пример #8
0
        public async Task UpdateJsonConfigAsync(GroupCommandMatch method, [Remainder] JObject jsonConfig)
        {
            if (await CheckMissingMethodID(method))
            {
                return;
            }

            using var service = GetService <IGrillBotRepository>();

            var config = await service.Service.ConfigRepository.FindConfigAsync(Context.Guild.Id, method.Group, method.Command, false);

            if (config == null)
            {
                await ReplyAsync("Požadovaná metoda neexistuje.");

                return;
            }

            config.Config = jsonConfig;
            await service.Service.CommitAsync();

            await ReplyAsync("Metoda byla úspěšně aktualizována").ConfigureAwait(false);
        }
Пример #9
0
        public async Task AddPermissionAsync(GroupCommandMatch method, string targetID, int permType, int allowType)
        {
            if (await CheckMissingMethodID(method))
            {
                return;
            }

            await Context.Guild.SyncGuildAsync();

            if (string.Equals(targetID, "everyone", StringComparison.InvariantCultureIgnoreCase))
            {
                permType = (int)PermType.Everyone;
            }

            var id = Convert.ToUInt64(targetID);

            switch ((PermType)permType)
            {
            case PermType.Role:
                if (Context.Guild.GetRole(id) == null)
                {
                    await ReplyAsync("Taková role neexistuje");

                    return;
                }
                break;

            case PermType.User:
                if (await Context.Guild.GetUserFromGuildAsync(id) == null)
                {
                    await ReplyAsync("Takový uživatel neexistuje.");

                    return;
                }
                break;

            case PermType.Everyone when !string.Equals(targetID, "everyone", StringComparison.InvariantCultureIgnoreCase):
                await ReplyAsync("Pro povolení všem použij klíčové slovo `everyone` jako identifikátor.");

                return;
            }

            using var service = GetService <IGrillBotRepository>();
            var config = await service.Service.ConfigRepository.FindConfigAsync(Context.Guild.Id, method.Group, method.Command, true);

            if (config == null)
            {
                await ReplyAsync("Požadovaná metoda neexistuje.");

                return;
            }

            config.Permissions.Add(new MethodPerm()
            {
                AllowType = (AllowType)allowType,
                DiscordID = targetID,
                PermType  = (PermType)permType
            });

            await service.Service.CommitAsync();

            await ReplyAsync("Oprávnění bylo úspěšně přidáno.");
        }