Better help part 1

This commit is contained in:
Karolis2011
2019-06-19 23:59:48 +03:00
parent 4907fe44f2
commit ed5f4d729e
3 changed files with 113 additions and 20 deletions

View File

@@ -7,6 +7,8 @@ using System.Threading.Tasks;
using System.Linq;
using EventBot.Attributes;
using EventBot.Services;
using Discord.Addons.Interactive;
using EventBot.Misc;
namespace EventBot.Modules
{
@@ -22,6 +24,7 @@ namespace EventBot.Modules
[RequireOwner(Group = "Permission")]
[RequireContext(ContextType.Guild)]
[Command("prefix")]
[Name("Configure prefix")]
[Summary("Gets prefix.")]
public async Task PrefixCommand()
{
@@ -38,6 +41,7 @@ namespace EventBot.Modules
[RequireOwner(Group = "Permission")]
[RequireContext(ContextType.Guild)]
[Command("prefix")]
[Name("Configure prefix")]
[Summary("Sets prefix.")]
public async Task PrefixCommand(
[Summary("New prefix to set")] string newPrefix)
@@ -50,8 +54,9 @@ namespace EventBot.Modules
await ReplyAsync($"Prefix has been set to `{guildConfig.Prefix}`");
}
[Group("help")]
public class HelpModule : ModuleBase<SocketCommandContext>
public class HelpModule : InteractiveBase<SocketCommandContext>
{
private readonly CommandService _commands;
@@ -61,23 +66,47 @@ namespace EventBot.Modules
}
[Command]
[Name("Help")]
[Summary("Lists all commands with there descriptions.")]
public async Task DefaultHelpAsync()
{
var embed = new EmbedBuilder()
.WithTitle("Command list")
.WithColor(Color.DarkBlue)
.WithCurrentTimestamp()
.WithFields(_commands.Commands
.Where(c => c.Attributes.Where(a => a is NoHelpAttribute || (a is RequireContextAttribute requireContext)).Count() == 0)
.Select(c =>
new EmbedFieldBuilder()
var pagedCommands = _commands.Commands
.OrderBy(c => c.Aliases[0])
.Where(c => c.Attributes
.Select(a => {
switch (a)
{
Name = $"`{string.Join(", ", c.Aliases)} {string.Join(" ", c.Parameters.Select(p => p.IsOptional ? $"[{p.Name}]" : $"<{p.Name}>"))}`",
Value = c.Summary
})
);
await Context.User.SendMessageAsync(embed: embed.Build());
case NoHelpAttribute _:
return false;
case RequireContextAttribute rc:
return Context.IsContextType(rc.Contexts);
default:
return true;
}
})
.Aggregate(true, (a, r) => a && r))
.SelectMany(c => c.Aliases.Select(a => new { CI = c, MA = (c.Aliases[0] == a), A = a}).Reverse())
.Select((e, i) => new { Command = e, Index = i })
.GroupBy(o => o.Index / 20)
.Select(g => g.Select(o => o.Command));
var pager = new PaginatedMessage()
{
Title = "Command list",
Color = Color.DarkBlue,
Pages = pagedCommands.Select(p => string.Join("\r\n", p.Select(c => {
if (c.MA)
return $"`{c.CI.FormatCallHelpString()}` - **{c.CI.Name}**";
else
return $"`{c.A}` `↪`";
}))),
Options = new PaginatedAppearanceOptions()
{
Info = null,
JumpDisplayOptions = JumpDisplayOptions.Never
}
};
await PagedReplyAsync(pager);
}
}
}