コード例 #1
0
            public async Task HelpAsync()
            {
                List <EmbedBuilder> embeds = new List <EmbedBuilder>();

                foreach (object value in Enum.GetValues(typeof(CommandCategory)))
                {
                    CommandCategory category = (CommandCategory)value;
                    if (Server != null && Server.DisabledCategories.Contains(category))
                    {
                        continue;
                    }

                    EmbedBuilder embed = NormalizeEmbed($"{category.Humanize(LetterCasing.Title)} Commands",
                                                        $"Use {"help <command/category>".InlineCode()} to see more information about a specific command/categoory" +
                                                        $"\n\n{"< >".InlineCode()} indicates a required parameter\n{"( )".InlineCode()} indicates an optional parameter");
                    IEnumerable <ModuleInfo> modules = _commands.Modules.Where(x =>
                                                                               x.Attributes.OfType <CommandCategoryAttribute>().FirstOrDefault()?.Category == category);
                    switch (modules.Count())
                    {
                    case 1:
                    {
                        foreach (ModuleInfo module in modules)
                        {
                            if (string.IsNullOrWhiteSpace(module.Group))
                            {
                                foreach (CommandInfo commandInfo in module.Commands)
                                {
                                    embed.AddField($"{commandInfo.GetName().Humanize(LetterCasing.Title)}",
                                                   $"{commandInfo.Summary}\n{commandInfo.GetUsage(Context).InlineCode()}");
                                }
                            }
                            else
                            {
                                embed.AddField($"{module.Group.Humanize(LetterCasing.Title)}",
                                               $"{module.Summary}\n{module.GetUsage(Context).InlineCode()}");
                            }
                        }

                        embeds.Add(embed);
                        break;
                    }

                    default:
                    {
                        List <EmbedBuilder> pageEmbeds = new List <EmbedBuilder>();
                        foreach (IEnumerable <ModuleInfo> batchedModules in modules.Batch(4))
                        {
                            foreach (ModuleInfo module in batchedModules)
                            {
                                if (string.IsNullOrWhiteSpace(module.Group))
                                {
                                    foreach (CommandInfo commandInfo in module.Commands)
                                    {
                                        embed.AddField($"{commandInfo.GetName().Humanize(LetterCasing.Title)}",
                                                       $"{commandInfo.Summary}\n{commandInfo.GetUsage(Context).InlineCode()}");
                                    }
                                }
                                else
                                {
                                    embed.AddField($"{module.Group.Humanize(LetterCasing.Title)}",
                                                   $"{module.Summary}\n{module.GetUsage(Context).InlineCode()}");
                                }
                            }

                            EmbedBuilder pageEmbed = NormalizeEmbed(embed);
                            pageEmbeds.Add(pageEmbed);
                        }
                        embeds.AddRange(pageEmbeds);
                        break;
                    }
                    }
                }
                switch (embeds.Count)
                {
                case 1:
                    await ReplyAsync(embed : embeds.First().Build());

                    break;

                default:
                    embeds = MoreEnumerable.ToHashSet(embeds).ToList();
                    await PagedReplyAsync(embeds);

                    break;
                }
            }
コード例 #2
0
            public async Task HelpAsync(string command)
            {
                SearchResult result = _commands.Search(command);
                EmbedBuilder embed  = new EmbedBuilder();

                if (!result.IsSuccess)
                {
                    if (Enum.TryParse(enumType: typeof(CommandCategory), value: command, ignoreCase: true, result: out object categoryObj))
                    {
                        CommandCategory category = (CommandCategory)categoryObj;
                        if (Server != null && Server.DisabledCategories.Contains(category))
                        {
                            await ReplyEmbedAsync("Unknown command",
                                                  $"Couldn't find a command or category for {command}");

                            return;
                        }
                        IEnumerable <ModuleInfo> modules = _commands.Modules.Where(x =>
                                                                                   x.Attributes.OfType <CommandCategoryAttribute>().FirstOrDefault()?.Category == category);
                        if (modules.Count() <= 8)
                        {
                            embed.WithTitle($"{category.Humanize(LetterCasing.Title)} Commands")
                            .WithColor(Color.Purple);
                            foreach (ModuleInfo module in modules)
                            {
                                if (string.IsNullOrWhiteSpace(module.Group))
                                {
                                    foreach (CommandInfo commandInfo in module.Commands)
                                    {
                                        embed.AddField($"{commandInfo.GetName().Humanize(LetterCasing.Title)}",
                                                       $"{commandInfo.Summary}\n{commandInfo.GetUsage(Context).InlineCode()}");
                                    }
                                }
                                else
                                {
                                    embed.AddField($"{module.Group.Humanize(LetterCasing.Title)}",
                                                   $"{module.Summary}\n{module.GetUsage(Context).InlineCode()}");
                                }
                            }

                            await ReplyEmbedAsync(embed);
                        }
                        else
                        {
                            List <EmbedBuilder> embeds = new List <EmbedBuilder>();
                            foreach (IEnumerable <ModuleInfo> mdls in modules.Batch(8))
                            {
                                foreach (ModuleInfo module in mdls)
                                {
                                    if (string.IsNullOrWhiteSpace(module.Group))
                                    {
                                        foreach (CommandInfo commandInfo in module.Commands)
                                        {
                                            embed.AddField($"{commandInfo.GetName().Humanize(LetterCasing.Title)}",
                                                           $"{commandInfo.Summary}\n{commandInfo.GetUsage(Context).InlineCode()}");
                                        }
                                    }
                                    else
                                    {
                                        embed.AddField($"{module.Group.Humanize(LetterCasing.Title)}",
                                                       $"{module.Summary}\n{module.GetUsage(Context).InlineCode()}");
                                    }
                                }

                                EmbedBuilder embd = NormalizeEmbed($"{category.Humanize(LetterCasing.Title)} Commands", null);
                                embeds.Add(embd);
                            }
                            embeds = MoreEnumerable.ToHashSet(embeds).ToList();
                            await PagedReplyAsync(embeds);
                        }
                        return;
                    }
                    await ReplyEmbedAsync("Unknown command",
                                          $"Couldn't find a command or category for {command}");

                    return;
                }
                CommandInfo     specificCommand  = result.Commands.First().Command;
                CommandCategory?specificCategory =
                    specificCommand.Attributes.OfType <CommandCategoryAttribute>().FirstOrDefault()?.Category ??
                    specificCommand.Module.Attributes.OfType <CommandCategoryAttribute>().FirstOrDefault()?.Category;

                if (specificCategory.HasValue && Server.DisabledCategories.Contains(specificCategory.Value))
                {
                    await ReplyEmbedAsync("Unknown command",
                                          $"Couldn't find a command or category for {command}");

                    return;
                }
                embed.WithTitle($"{command.Humanize(LetterCasing.Title)} Command Help")
                .WithDescription(result.Commands.First().Command.Summary)
                .AddField("Usage",
                          string.IsNullOrWhiteSpace(result.Commands.First().Command.Module.Group)
                            ? result.Commands.Select(x => x.Command).GetUsage(Context).InlineCode()
                            : result.Commands.First().Command.Module.GetUsage(Context).InlineCode());
                if (specificCommand.Aliases.Count > 1)
                {
                    embed.AddField("Aliases",
                                   string.Join(", ", specificCommand.Aliases.Select(Formatter.InlineCode)));
                }

                await ReplyEmbedAsync(embed);
            }