Example #1
0
        private static async Task _showHelpCategory(ICommandContext context, CommandHelpInfoCollection commands, string category)
        {
            SortedDictionary <string, List <CommandHelpInfo> > commands_lists = new SortedDictionary <string, List <CommandHelpInfo> >();

            foreach (CommandHelpInfo command in commands)
            {
                if (!commands_lists.ContainsKey(command.Category))
                {
                    commands_lists[command.Category] = new List <CommandHelpInfo>();
                }

                commands_lists[command.Category].Add(command);
            }

            EmbedBuilder  builder             = new EmbedBuilder();
            StringBuilder description_builder = new StringBuilder();

            if (!string.IsNullOrEmpty(category))
            {
                description_builder.AppendLine(string.Format("Commands listed must be prefaced with `{0}` (e.g. `{2}{0} {1}`).",
                                                             category,
                                                             commands.First().Name,
                                                             OurFoodChainBot.Instance.Config.Prefix));
            }

            description_builder.AppendLine(string.Format("To learn more about a command, use `{0}help <command>` (e.g. `{0}help {1}{2}`).",
                                                         OurFoodChainBot.Instance.Config.Prefix,
                                                         string.IsNullOrEmpty(category) ? "" : category + " ",
                                                         commands.First().Name));

            string version_string = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();

            // Remove any trailing ".0"s from the version number, so they don't appear when unused.
            while (version_string.EndsWith(".0"))
            {
                version_string = version_string.Substring(0, version_string.Length - 2);
            }

            builder.WithTitle("Commands list");
            builder.WithDescription(description_builder.ToString());
            builder.WithFooter(string.Format("ourfoodchain-bot v.{0} — github.com/gsemac/ourfoodchain-bot", version_string));

            foreach (string cat in commands_lists.Keys)
            {
                // Sort commands, and filter out commands that aren't currently loaded.
                // As well, filter out commands that the current user does not have access to.

                List <string> command_str_list = new List <string>();

                foreach (CommandHelpInfo c in commands_lists[cat])
                {
                    if (await Bot.CommandUtils.CommandIsEnabledAsync(context, c.Name))
                    {
                        command_str_list.Add(string.Format("`{0}`", c.Name));
                    }
                }

                command_str_list.Sort((lhs, rhs) => lhs.CompareTo(rhs));

                if (command_str_list.Count() > 0)
                {
                    builder.AddField(StringUtils.ToTitleCase(cat), string.Join("  ", command_str_list));
                }
            }

            await context.Channel.SendMessageAsync("", false, builder.Build());
        }