2017-07-20 03:10:39 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
using Discord;
|
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using System;
|
2017-06-12 12:26:14 +00:00
|
|
|
|
using Discord.Commands;
|
|
|
|
|
using NadekoBot.Extensions;
|
|
|
|
|
using System.Linq;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common.Attributes;
|
|
|
|
|
using NadekoBot.Common.ModuleBehaviors;
|
2017-10-13 04:14:54 +00:00
|
|
|
|
using NadekoBot.Core.Services;
|
|
|
|
|
using NadekoBot.Core.Services.Impl;
|
2017-11-15 19:39:51 +00:00
|
|
|
|
using NadekoBot.Common;
|
|
|
|
|
using NLog;
|
2017-11-29 06:53:16 +00:00
|
|
|
|
using CommandLine;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-07-17 02:37:51 +00:00
|
|
|
|
namespace NadekoBot.Modules.Help.Services
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public class HelpService : ILateExecutor, INService
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-07-20 03:10:39 +00:00
|
|
|
|
private readonly IBotConfigProvider _bc;
|
2017-06-12 12:26:14 +00:00
|
|
|
|
private readonly CommandHandler _ch;
|
|
|
|
|
private readonly NadekoStrings _strings;
|
2017-11-15 19:39:51 +00:00
|
|
|
|
private readonly Logger _log;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-07-20 03:10:39 +00:00
|
|
|
|
public HelpService(IBotConfigProvider bc, CommandHandler ch, NadekoStrings strings)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
|
|
|
|
_bc = bc;
|
2017-06-12 12:26:14 +00:00
|
|
|
|
_ch = ch;
|
|
|
|
|
_strings = strings;
|
2017-11-15 19:39:51 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-15 19:39:51 +00:00
|
|
|
|
public Task LateExecute(DiscordSocketClient client, IGuild guild, IUserMessage msg)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-11-15 19:39:51 +00:00
|
|
|
|
if (guild == null)
|
|
|
|
|
{
|
|
|
|
|
if (CREmbed.TryParse(_bc.BotConfig.DMHelpString, out var embed))
|
|
|
|
|
return msg.Channel.EmbedAsync(embed.ToEmbed(), embed.PlainText?.SanitizeMentions() ?? "");
|
|
|
|
|
|
|
|
|
|
return msg.Channel.SendMessageAsync(_bc.BotConfig.DMHelpString);
|
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
2017-11-15 19:39:51 +00:00
|
|
|
|
catch (Exception ex)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-11-15 19:39:51 +00:00
|
|
|
|
_log.Warn(ex);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
2017-11-15 19:39:51 +00:00
|
|
|
|
return Task.CompletedTask;
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
2017-06-12 12:26:14 +00:00
|
|
|
|
|
|
|
|
|
public EmbedBuilder GetCommandHelp(CommandInfo com, IGuild guild)
|
|
|
|
|
{
|
|
|
|
|
var prefix = _ch.GetPrefix(guild);
|
|
|
|
|
|
|
|
|
|
var str = string.Format("**`{0}`**", prefix + com.Aliases.First());
|
|
|
|
|
var alias = com.Aliases.Skip(1).FirstOrDefault();
|
|
|
|
|
if (alias != null)
|
|
|
|
|
str += string.Format(" **/ `{0}`**", prefix + alias);
|
2017-11-29 06:53:16 +00:00
|
|
|
|
var em = new EmbedBuilder()
|
2017-06-12 12:26:14 +00:00
|
|
|
|
.AddField(fb => fb.WithName(str).WithValue($"{com.RealSummary(prefix)} {GetCommandRequirements(com, guild)}").WithIsInline(true))
|
|
|
|
|
.AddField(fb => fb.WithName(GetText("usage", guild)).WithValue(com.RealRemarks(prefix)).WithIsInline(false))
|
2017-06-28 01:48:55 +00:00
|
|
|
|
.WithFooter(efb => efb.WithText(GetText("module", guild, com.Module.GetTopLevelModule().Name)))
|
2017-06-12 12:26:14 +00:00
|
|
|
|
.WithColor(NadekoBot.OkColor);
|
2017-11-29 06:53:16 +00:00
|
|
|
|
|
|
|
|
|
var opt = (NadekoOptions)com.Attributes.FirstOrDefault(x => x is NadekoOptions);
|
|
|
|
|
if (opt != null)
|
|
|
|
|
{
|
|
|
|
|
var x = Activator.CreateInstance(opt.OptionType);
|
|
|
|
|
var hs = Parser.Default.FormatCommandLine(x);
|
|
|
|
|
if(!string.IsNullOrWhiteSpace(hs))
|
|
|
|
|
em.AddField(GetText("options", guild), string.Join("\n--", hs.Split(" --")), false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return em;
|
2017-06-12 12:26:14 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetCommandRequirements(CommandInfo cmd, IGuild guild) =>
|
|
|
|
|
string.Join(" ", cmd.Preconditions
|
|
|
|
|
.Where(ca => ca is OwnerOnlyAttribute || ca is RequireUserPermissionAttribute)
|
|
|
|
|
.Select(ca =>
|
|
|
|
|
{
|
|
|
|
|
if (ca is OwnerOnlyAttribute)
|
|
|
|
|
return Format.Bold(GetText("bot_owner_only", guild));
|
|
|
|
|
var cau = (RequireUserPermissionAttribute)ca;
|
|
|
|
|
if (cau.GuildPermission != null)
|
|
|
|
|
return Format.Bold(GetText("server_permission", guild, cau.GuildPermission))
|
|
|
|
|
.Replace("Guild", "Server");
|
|
|
|
|
return Format.Bold(GetText("channel_permission", guild, cau.ChannelPermission))
|
|
|
|
|
.Replace("Guild", "Server");
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
private string GetText(string text, IGuild guild, params object[] replacements) =>
|
|
|
|
|
_strings.GetText(text, guild?.Id, "Help".ToLowerInvariant(), replacements);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
|
|
|
|
}
|