NadekoBot/NadekoBot.Core/Modules/Help/Help.cs

185 lines
7.8 KiB
C#
Raw Normal View History

2016-08-16 14:53:38 +00:00
using Discord.Commands;
using NadekoBot.Extensions;
using System.Linq;
using Discord;
using NadekoBot.Core.Services;
2016-08-16 14:53:38 +00:00
using System.Threading.Tasks;
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
2017-07-17 02:37:51 +00:00
using NadekoBot.Modules.Help.Services;
2017-07-17 19:42:36 +00:00
using NadekoBot.Modules.Permissions.Services;
using NadekoBot.Common;
using NadekoBot.Common.Replacements;
2016-08-16 14:53:38 +00:00
namespace NadekoBot.Modules.Help
{
2017-07-15 16:34:34 +00:00
public class Help : NadekoTopLevelModule<HelpService>
2016-08-16 14:53:38 +00:00
{
public const string PatreonUrl = "https://patreon.com/nadekobot";
public const string PaypalUrl = "https://paypal.me/Kwoth";
2017-05-24 04:43:00 +00:00
private readonly IBotCredentials _creds;
private readonly IBotConfigProvider _config;
2017-05-24 04:43:00 +00:00
private readonly CommandService _cmds;
private readonly GlobalPermissionService _perms;
2017-05-24 04:43:00 +00:00
public EmbedBuilder GetHelpStringEmbed()
{
var r = new ReplacementBuilder()
.WithDefault(Context)
.WithOverride("{0}", () => _creds.ClientId.ToString())
.WithOverride("{1}", () => Prefix)
.Build();
if (!CREmbed.TryParse(_config.BotConfig.HelpString, out var embed))
return new EmbedBuilder().WithOkColor()
.WithDescription(String.Format(_config.BotConfig.HelpString, _creds.ClientId, Prefix));
r.Replace(embed);
return embed.ToEmbed();
}
2017-05-24 04:43:00 +00:00
public Help(IBotCredentials creds, GlobalPermissionService perms, IBotConfigProvider config, CommandService cmds)
2017-05-24 04:43:00 +00:00
{
_creds = creds;
_config = config;
_cmds = cmds;
_perms = perms;
2017-05-24 04:43:00 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-12-16 18:43:57 +00:00
public async Task Modules()
2016-08-16 14:53:38 +00:00
{
var embed = new EmbedBuilder().WithOkColor()
.WithFooter(efb => efb.WithText("" + GetText("modules_footer", Prefix)))
.WithTitle(GetText("list_of_modules"))
.WithDescription(string.Join("\n",
2017-05-24 04:43:00 +00:00
_cmds.Modules.GroupBy(m => m.GetTopLevelModule())
.Where(m => !_perms.BlockedModules.Contains(m.Key.Name.ToLowerInvariant()))
.Select(m => "• " + m.Key.Name)
.OrderBy(s => s)));
2016-12-31 10:55:12 +00:00
await Context.Channel.EmbedAsync(embed).ConfigureAwait(false);
2016-08-16 14:53:38 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
public async Task Commands([Remainder] string module = null)
2016-08-16 14:53:38 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = Context.Channel;
2016-08-16 14:53:38 +00:00
module = module?.Trim().ToUpperInvariant();
if (string.IsNullOrWhiteSpace(module))
return;
2017-05-24 04:43:00 +00:00
var cmds = _cmds.Commands.Where(c => c.Module.GetTopLevelModule().Name.ToUpperInvariant().StartsWith(module))
.Where(c => !_perms.BlockedCommands.Contains(c.Aliases.First().ToLowerInvariant()))
2016-12-16 19:45:46 +00:00
.OrderBy(c => c.Aliases.First())
.Distinct(new CommandTextEqualityComparer())
2016-08-16 14:53:38 +00:00
.AsEnumerable();
2016-12-16 19:45:46 +00:00
var cmdsArray = cmds as CommandInfo[] ?? cmds.ToArray();
2016-08-16 14:53:38 +00:00
if (!cmdsArray.Any())
{
await ReplyErrorLocalized("module_not_found").ConfigureAwait(false);
2016-08-16 14:53:38 +00:00
return;
}
var j = 0;
var groups = cmdsArray.GroupBy(x => j++ / 48).ToArray();
for (int i = 0; i < groups.Count(); i++)
{
2017-05-24 04:43:00 +00:00
await channel.SendTableAsync(i == 0 ? $"📃 **{GetText("list_of_commands")}**\n" : "", groups.ElementAt(i), el => $"{Prefix + el.Aliases.First(),-15} {"[" + el.Aliases.Skip(1).FirstOrDefault() + "]",-8}").ConfigureAwait(false);
}
await ConfirmLocalized("commands_instr", Prefix).ConfigureAwait(false);
2016-08-16 14:53:38 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[Priority(0)]
public async Task H([Remainder] string fail)
{
await ReplyErrorLocalized("command_not_found").ConfigureAwait(false);
}
2016-08-16 14:53:38 +00:00
[NadekoCommand, Usage, Description, Aliases]
[Priority(1)]
2017-05-24 04:43:00 +00:00
public async Task H([Remainder] CommandInfo com = null)
2016-08-16 14:53:38 +00:00
{
2016-12-16 18:43:57 +00:00
var channel = Context.Channel;
2016-08-16 14:53:38 +00:00
2017-05-24 04:43:00 +00:00
if (com == null)
2016-08-16 14:53:38 +00:00
{
2017-07-05 15:38:38 +00:00
IMessageChannel ch = channel is ITextChannel ? await ((IGuildUser)Context.User).GetOrCreateDMChannelAsync() : channel;
await ch.EmbedAsync(GetHelpStringEmbed()).ConfigureAwait(false);
2016-08-16 14:53:38 +00:00
return;
}
2017-07-15 16:34:34 +00:00
var embed = _service.GetCommandHelp(com, Context.Guild);
2016-12-16 18:43:57 +00:00
await channel.EmbedAsync(embed).ConfigureAwait(false);
2016-08-16 14:53:38 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-08-16 14:53:38 +00:00
[RequireContext(ContextType.Guild)]
[OwnerOnly]
2017-01-04 12:47:36 +00:00
public async Task Hgit()
2016-08-16 14:53:38 +00:00
{
var helpstr = new StringBuilder();
helpstr.AppendLine(GetText("cmdlist_donate", PatreonUrl, PaypalUrl) + "\n");
helpstr.AppendLine("##"+ GetText("table_of_contents"));
2017-05-24 04:43:00 +00:00
helpstr.AppendLine(string.Join("\n", _cmds.Modules.Where(m => m.GetTopLevelModule().Name.ToLowerInvariant() != "help")
2017-01-04 13:31:17 +00:00
.Select(m => m.GetTopLevelModule().Name)
.Distinct()
.OrderBy(m => m)
.Prepend("Help")
.Select(m => string.Format("- [{0}](#{1})", m, m.ToLowerInvariant()))));
2016-10-28 00:04:14 +00:00
helpstr.AppendLine();
2016-10-28 00:09:16 +00:00
string lastModule = null;
2017-05-24 04:43:00 +00:00
foreach (var com in _cmds.Commands.OrderBy(com => com.Module.GetTopLevelModule().Name).GroupBy(c => c.Aliases.First()).Select(g => g.First()))
2016-08-16 14:53:38 +00:00
{
2017-01-04 12:47:36 +00:00
var module = com.Module.GetTopLevelModule();
if (module.Name != lastModule)
2016-08-16 14:53:38 +00:00
{
2016-10-28 00:09:16 +00:00
if (lastModule != null)
2016-10-28 00:17:22 +00:00
{
helpstr.AppendLine();
helpstr.AppendLine($"###### [{GetText("back_to_toc")}](#{GetText("table_of_contents").ToLowerInvariant().Replace(' ', '-')})");
2016-10-28 00:17:22 +00:00
}
2016-10-28 00:04:14 +00:00
helpstr.AppendLine();
2017-01-04 12:47:36 +00:00
helpstr.AppendLine("### " + module.Name + " ");
helpstr.AppendLine($"{GetText("cmd_and_alias")} | {GetText("desc")} | {GetText("usage")}");
2016-08-16 14:53:38 +00:00
helpstr.AppendLine("----------------|--------------|-------");
2017-01-04 12:47:36 +00:00
lastModule = module.Name;
2016-08-16 14:53:38 +00:00
}
2017-06-13 00:55:42 +00:00
helpstr.AppendLine($"{string.Join(" ", com.Aliases.Select(a => "`" + Prefix + a + "`"))} |" +
2017-07-15 16:34:34 +00:00
$" {string.Format(com.Summary, Prefix)} {_service.GetCommandRequirements(com, Context.Guild)} |" +
$" {string.Format(com.Remarks, Prefix)}");
2016-08-16 14:53:38 +00:00
}
2016-10-09 22:44:50 +00:00
File.WriteAllText("../../docs/Commands List.md", helpstr.ToString());
await ReplyConfirmLocalized("commandlist_regen").ConfigureAwait(false);
2016-08-16 14:53:38 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-12-16 18:43:57 +00:00
public async Task Guide()
2016-08-16 14:53:38 +00:00
{
await ConfirmLocalized("guide",
2017-06-29 16:40:13 +00:00
"http://nadekobot.readthedocs.io/en/latest/Commands%20List/",
"http://nadekobot.readthedocs.io/en/latest/").ConfigureAwait(false);
2016-08-16 14:53:38 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
2016-12-16 18:43:57 +00:00
public async Task Donate()
2016-08-16 14:53:38 +00:00
{
await ReplyConfirmLocalized("donate", PatreonUrl, PaypalUrl).ConfigureAwait(false);
2016-08-16 14:53:38 +00:00
}
}
2016-12-16 19:45:46 +00:00
public class CommandTextEqualityComparer : IEqualityComparer<CommandInfo>
{
2016-12-16 19:45:46 +00:00
public bool Equals(CommandInfo x, CommandInfo y) => x.Aliases.First() == y.Aliases.First();
2016-12-16 19:45:46 +00:00
public int GetHashCode(CommandInfo obj) => obj.Aliases.First().GetHashCode();
}
2016-11-18 19:24:25 +00:00
}