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-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-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-05-29 04:13:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
public async Task LateExecute(DiscordSocketClient client, IGuild guild, IUserMessage msg)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if(guild == null)
|
2017-07-20 03:10:39 +00:00
|
|
|
|
await msg.Channel.SendMessageAsync(_bc.BotConfig.DMHelpString).ConfigureAwait(false);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
//ignore
|
|
|
|
|
}
|
|
|
|
|
}
|
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);
|
|
|
|
|
return new EmbedBuilder()
|
|
|
|
|
.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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|