コード例 #1
0
        public async Task HelpAsync([Summary("The command for which to get help.")]
                                    string command)
        {
            // Deletes the invoking message if it's not a direct message.
            if (!Context.IsPrivate)
            {
                await Context.Message.DeleteAsync();
            }

            var result = _commands.Search(Context, command);

            if (!result.IsSuccess)
            {
                await ReplyAsync($"No commands matching **{command}** were found.");

                return;
            }

            // Iterates command search results.
            for (var i = 0; i < result.Commands.Count; ++i)
            {
                var cmd = result.Commands[i].Command;

                var parameters  = _help.GetParameters(cmd.Parameters);
                var contexts    = _help.GetContexts(cmd.Preconditions);
                var permissions = _help.GetPermissions(cmd.Preconditions);
                var roles       = _help.GetRoles(cmd.Preconditions, Context);

                // Creates the embed.
                var embed = new EmbedBuilder
                {
                    Color       = new Color(47, 111, 146),
                    Title       = $"\u2753 `{cmd.Name}` Help",
                    Description = $"`{_help.GetUsage(cmd)}`\n{cmd.Summary}"
                };

                // Only includes result count if there's more than one.
                // Only includes message about optional parameters if the command has any.
                embed.WithFooter(
                    (result.Commands.Count > 1 ? $"Result {i + 1}/{result.Commands.Count}." : string.Empty) +
                    (cmd.Parameters.Any(p => p.IsOptional)
                        ? " Angle brackets and italics denote optional arguments/parameters."
                        : string.Empty));

                if (!string.IsNullOrWhiteSpace(cmd.Remarks))
                {
                    embed.AddField("Details", cmd.Remarks);
                }

                if (!string.IsNullOrWhiteSpace(parameters))
                {
                    embed.AddField("Parameters", parameters);
                }

                if (!string.IsNullOrWhiteSpace(contexts))
                {
                    embed.AddField("Contexts", contexts, true);
                }

                if (!string.IsNullOrWhiteSpace(permissions))
                {
                    embed.AddField("Permissions", permissions, true);
                }

                if (!string.IsNullOrWhiteSpace(roles))
                {
                    embed.AddField("Roles", roles, true);
                }

                // Excludes the command's name from the aliases.
                if (cmd.Aliases.Count > 1)
                {
                    embed.AddField(
                        "Aliases",
                        string.Join("\n",
                                    cmd.Aliases.Where(a => !a.Equals(cmd.Name, StringComparison.OrdinalIgnoreCase))), true);
                }

                // Replies normally if a direct message fails.
                try
                {
                    await Context.User.SendMessageAsync(string.Empty, false, embed.Build());
                }
                catch
                {
                    await ReplyAsync(string.Empty, false, embed.Build());
                }
            }
        }