2016-08-29 22:47:04 +00:00
|
|
|
|
using Discord.WebSocket;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord;
|
|
|
|
|
using NLog;
|
|
|
|
|
using Discord.Commands;
|
2016-10-03 17:21:05 +00:00
|
|
|
|
using NadekoBot.Extensions;
|
2016-12-31 16:34:21 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2017-01-06 23:46:07 +00:00
|
|
|
|
using System.Threading;
|
2017-05-19 08:02:29 +00:00
|
|
|
|
using System.Collections.Immutable;
|
2017-05-30 05:03:35 +00:00
|
|
|
|
using System.IO;
|
2017-06-01 03:12:00 +00:00
|
|
|
|
using Discord.Net;
|
2017-07-17 19:42:36 +00:00
|
|
|
|
using NadekoBot.Common;
|
|
|
|
|
using NadekoBot.Common.Collections;
|
|
|
|
|
using NadekoBot.Common.ModuleBehaviors;
|
2016-08-29 22:47:04 +00:00
|
|
|
|
|
2017-10-13 04:14:54 +00:00
|
|
|
|
namespace NadekoBot.Core.Services
|
2016-08-29 22:47:04 +00:00
|
|
|
|
{
|
2017-02-26 21:05:17 +00:00
|
|
|
|
public class GuildUserComparer : IEqualityComparer<IGuildUser>
|
2016-10-10 08:06:06 +00:00
|
|
|
|
{
|
2016-12-17 04:09:04 +00:00
|
|
|
|
public bool Equals(IGuildUser x, IGuildUser y) => x.Id == y.Id;
|
2016-10-10 08:06:06 +00:00
|
|
|
|
|
2016-12-17 04:09:04 +00:00
|
|
|
|
public int GetHashCode(IGuildUser obj) => obj.Id.GetHashCode();
|
2016-10-10 08:06:06 +00:00
|
|
|
|
}
|
2017-07-15 03:04:16 +00:00
|
|
|
|
|
|
|
|
|
public class CommandHandler : INService
|
2016-08-29 22:47:04 +00:00
|
|
|
|
{
|
2017-01-18 20:52:42 +00:00
|
|
|
|
public const int GlobalCommandsCooldown = 750;
|
2017-01-07 21:31:42 +00:00
|
|
|
|
|
2017-06-19 13:42:10 +00:00
|
|
|
|
private readonly DiscordSocketClient _client;
|
2017-01-14 16:37:11 +00:00
|
|
|
|
private readonly CommandService _commandService;
|
|
|
|
|
private readonly Logger _log;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
private readonly IBotCredentials _creds;
|
|
|
|
|
private readonly NadekoBot _bot;
|
|
|
|
|
private INServiceProvider _services;
|
2017-10-30 13:38:50 +00:00
|
|
|
|
private IEnumerable<IEarlyBlocker> _earlyBlockers;
|
|
|
|
|
private IEarlyBlockingExecutor[] _earlyBlockingExecutors;
|
|
|
|
|
private IInputTransformer[] _inputTransformers;
|
|
|
|
|
private ILateBlocker[] _lateBlockers;
|
|
|
|
|
private ILateExecutor[] _lateExecutors;
|
|
|
|
|
|
2017-05-30 04:54:59 +00:00
|
|
|
|
public string DefaultPrefix { get; private set; }
|
|
|
|
|
private ConcurrentDictionary<ulong, string> _prefixes { get; } = new ConcurrentDictionary<ulong, string>();
|
2016-08-29 22:47:04 +00:00
|
|
|
|
|
2017-09-10 01:52:34 +00:00
|
|
|
|
private ImmutableArray<AsyncLazy<IDMChannel>> OwnerChannels { get; set; } = new ImmutableArray<AsyncLazy<IDMChannel>>();
|
2017-06-07 06:34:03 +00:00
|
|
|
|
|
2017-03-31 12:07:18 +00:00
|
|
|
|
public event Func<IUserMessage, CommandInfo, Task> CommandExecuted = delegate { return Task.CompletedTask; };
|
2017-06-12 12:26:14 +00:00
|
|
|
|
public event Func<CommandInfo, ITextChannel, string, Task> CommandErrored = delegate { return Task.CompletedTask; };
|
2017-08-22 03:47:57 +00:00
|
|
|
|
public event Func<IUserMessage, Task> OnMessageNoTrigger = delegate { return Task.CompletedTask; };
|
2016-08-30 00:17:21 +00:00
|
|
|
|
|
2016-12-31 16:34:21 +00:00
|
|
|
|
//userid/msg count
|
|
|
|
|
public ConcurrentDictionary<ulong, uint> UserMessagesSent { get; } = new ConcurrentDictionary<ulong, uint>();
|
|
|
|
|
|
2017-01-06 23:46:07 +00:00
|
|
|
|
public ConcurrentHashSet<ulong> UsersOnShortCooldown { get; } = new ConcurrentHashSet<ulong>();
|
2017-02-26 21:05:17 +00:00
|
|
|
|
private readonly Timer _clearUsersOnShortCooldown;
|
2017-01-06 23:46:07 +00:00
|
|
|
|
|
2017-10-13 00:21:39 +00:00
|
|
|
|
public CommandHandler(DiscordSocketClient client, DbService db,
|
|
|
|
|
IBotConfigProvider bc, CommandService commandService,
|
|
|
|
|
IBotCredentials credentials, NadekoBot bot)
|
2016-08-29 22:47:04 +00:00
|
|
|
|
{
|
|
|
|
|
_client = client;
|
|
|
|
|
_commandService = commandService;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
_creds = credentials;
|
|
|
|
|
_bot = bot;
|
2017-05-30 04:54:59 +00:00
|
|
|
|
_db = db;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
2016-08-29 22:47:04 +00:00
|
|
|
|
_log = LogManager.GetCurrentClassLogger();
|
2017-01-06 23:46:07 +00:00
|
|
|
|
|
2017-02-26 21:05:17 +00:00
|
|
|
|
_clearUsersOnShortCooldown = new Timer(_ =>
|
2017-01-06 23:46:07 +00:00
|
|
|
|
{
|
|
|
|
|
UsersOnShortCooldown.Clear();
|
2017-01-07 21:31:42 +00:00
|
|
|
|
}, null, GlobalCommandsCooldown, GlobalCommandsCooldown);
|
2017-05-30 04:54:59 +00:00
|
|
|
|
|
2017-07-20 03:10:39 +00:00
|
|
|
|
DefaultPrefix = bc.BotConfig.DefaultPrefix;
|
2017-10-13 00:21:39 +00:00
|
|
|
|
_prefixes = bot.AllGuildConfigs
|
2017-05-30 04:54:59 +00:00
|
|
|
|
.Where(x => x.Prefix != null)
|
|
|
|
|
.ToDictionary(x => x.GuildId, x => x.Prefix)
|
|
|
|
|
.ToConcurrent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string GetPrefix(IGuild guild) => GetPrefix(guild?.Id);
|
|
|
|
|
|
|
|
|
|
public string GetPrefix(ulong? id)
|
|
|
|
|
{
|
|
|
|
|
if (id == null || !_prefixes.TryGetValue(id.Value, out var prefix))
|
|
|
|
|
return DefaultPrefix;
|
|
|
|
|
|
|
|
|
|
return prefix;
|
2016-10-10 08:06:06 +00:00
|
|
|
|
}
|
2017-05-02 18:16:34 +00:00
|
|
|
|
|
2017-05-30 04:54:59 +00:00
|
|
|
|
public string SetDefaultPrefix(string prefix)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(prefix))
|
|
|
|
|
throw new ArgumentNullException(nameof(prefix));
|
|
|
|
|
|
|
|
|
|
prefix = prefix.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
uow.BotConfig.GetOrCreate(set => set).DefaultPrefix = prefix;
|
|
|
|
|
uow.Complete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return DefaultPrefix = prefix;
|
|
|
|
|
}
|
|
|
|
|
public string SetPrefix(IGuild guild, string prefix)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(prefix))
|
|
|
|
|
throw new ArgumentNullException(nameof(prefix));
|
|
|
|
|
if (guild == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(guild));
|
|
|
|
|
|
|
|
|
|
prefix = prefix.ToLowerInvariant();
|
|
|
|
|
|
|
|
|
|
using (var uow = _db.UnitOfWork)
|
|
|
|
|
{
|
|
|
|
|
var gc = uow.GuildConfigs.For(guild.Id, set => set);
|
|
|
|
|
gc.Prefix = prefix;
|
|
|
|
|
uow.Complete();
|
|
|
|
|
}
|
|
|
|
|
_prefixes.AddOrUpdate(guild.Id, prefix, (key, old) => prefix);
|
|
|
|
|
|
|
|
|
|
return prefix;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-22 23:59:31 +00:00
|
|
|
|
public void AddServices(INServiceProvider services)
|
|
|
|
|
{
|
|
|
|
|
_services = services;
|
2017-10-30 13:38:50 +00:00
|
|
|
|
|
|
|
|
|
_earlyBlockers = services
|
|
|
|
|
.Select(x => x as IEarlyBlocker)
|
|
|
|
|
.Where(x => x != null)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
_earlyBlockingExecutors = services
|
|
|
|
|
.Select(x => x as IEarlyBlockingExecutor)
|
|
|
|
|
.Where(x => x != null)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
_inputTransformers = services
|
|
|
|
|
.Select(x => x as IInputTransformer)
|
|
|
|
|
.Where(x => x != null)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
_lateBlockers = services
|
|
|
|
|
.Select(x => x as ILateBlocker)
|
|
|
|
|
.Where(x => x != null)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
_lateExecutors = services
|
|
|
|
|
.Select(x => x as ILateExecutor)
|
|
|
|
|
.Where(x => x != null)
|
|
|
|
|
.ToArray();
|
2017-05-22 23:59:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 18:16:34 +00:00
|
|
|
|
public async Task ExecuteExternal(ulong? guildId, ulong channelId, string commandText)
|
|
|
|
|
{
|
2017-05-03 07:49:35 +00:00
|
|
|
|
if (guildId != null)
|
|
|
|
|
{
|
2017-05-22 23:59:31 +00:00
|
|
|
|
var guild = _client.GetGuild(guildId.Value);
|
2017-05-03 07:49:35 +00:00
|
|
|
|
var channel = guild?.GetChannel(channelId) as SocketTextChannel;
|
|
|
|
|
if (channel == null)
|
2017-05-02 18:16:34 +00:00
|
|
|
|
{
|
2017-05-03 07:49:35 +00:00
|
|
|
|
_log.Warn("Channel for external execution not found.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-02 18:16:34 +00:00
|
|
|
|
|
2017-05-03 07:49:35 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
IUserMessage msg = await channel.SendMessageAsync(commandText).ConfigureAwait(false);
|
|
|
|
|
msg = (IUserMessage)await channel.GetMessageAsync(msg.Id).ConfigureAwait(false);
|
|
|
|
|
await TryRunCommand(guild, channel, msg).ConfigureAwait(false);
|
|
|
|
|
//msg.DeleteAfter(5);
|
2017-05-02 18:16:34 +00:00
|
|
|
|
}
|
2017-05-03 07:49:35 +00:00
|
|
|
|
catch { }
|
|
|
|
|
}
|
2017-05-02 18:16:34 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-17 11:05:05 +00:00
|
|
|
|
public Task StartHandling()
|
2016-10-10 08:06:06 +00:00
|
|
|
|
{
|
2017-05-30 04:54:59 +00:00
|
|
|
|
_client.MessageReceived += (msg) => { var _ = Task.Run(() => MessageReceivedHandler(msg)); return Task.CompletedTask; };
|
2017-02-17 11:05:05 +00:00
|
|
|
|
return Task.CompletedTask;
|
2016-08-29 22:47:04 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-02-26 21:05:17 +00:00
|
|
|
|
private const float _oneThousandth = 1.0f / 1000;
|
2017-05-30 04:54:59 +00:00
|
|
|
|
private readonly DbService _db;
|
2017-05-22 23:59:31 +00:00
|
|
|
|
|
2017-05-30 04:54:59 +00:00
|
|
|
|
private Task LogSuccessfulExecution(IUserMessage usrMsg, ITextChannel channel, params int[] execPoints)
|
2017-01-07 21:31:42 +00:00
|
|
|
|
{
|
2017-10-29 19:06:48 +00:00
|
|
|
|
_log.Info($"S: {_client.ShardId} | Command Executed after " + string.Join("/", execPoints.Select(x => x * _oneThousandth)) + "s\n\t" +
|
2017-01-07 21:31:42 +00:00
|
|
|
|
"User: {0}\n\t" +
|
|
|
|
|
"Server: {1}\n\t" +
|
|
|
|
|
"Channel: {2}\n\t" +
|
|
|
|
|
"Message: {3}",
|
|
|
|
|
usrMsg.Author + " [" + usrMsg.Author.Id + "]", // {0}
|
|
|
|
|
(channel == null ? "PRIVATE" : channel.Guild.Name + " [" + channel.Guild.Id + "]"), // {1}
|
|
|
|
|
(channel == null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), // {2}
|
2017-05-22 23:59:31 +00:00
|
|
|
|
usrMsg.Content // {3}
|
2017-02-05 18:15:05 +00:00
|
|
|
|
);
|
2017-02-02 13:16:28 +00:00
|
|
|
|
return Task.CompletedTask;
|
2017-01-07 21:31:42 +00:00
|
|
|
|
}
|
2016-10-03 02:19:14 +00:00
|
|
|
|
|
2017-05-30 04:54:59 +00:00
|
|
|
|
private void LogErroredExecution(string errorMessage, IUserMessage usrMsg, ITextChannel channel, params int[] execPoints)
|
2017-01-07 21:31:42 +00:00
|
|
|
|
{
|
2017-10-29 19:06:48 +00:00
|
|
|
|
_log.Warn($"S: {_client.ShardId} | Command Errored after " + string.Join("/", execPoints.Select(x => x * _oneThousandth)) + "s\n\t" +
|
2017-01-18 20:52:42 +00:00
|
|
|
|
"User: {0}\n\t" +
|
|
|
|
|
"Server: {1}\n\t" +
|
|
|
|
|
"Channel: {2}\n\t" +
|
|
|
|
|
"Message: {3}\n\t" +
|
|
|
|
|
"Error: {4}",
|
|
|
|
|
usrMsg.Author + " [" + usrMsg.Author.Id + "]", // {0}
|
|
|
|
|
(channel == null ? "PRIVATE" : channel.Guild.Name + " [" + channel.Guild.Id + "]"), // {1}
|
|
|
|
|
(channel == null ? "PRIVATE" : channel.Name + " [" + channel.Id + "]"), // {2}
|
|
|
|
|
usrMsg.Content,// {3}
|
2017-05-30 04:54:59 +00:00
|
|
|
|
errorMessage
|
2017-05-25 02:24:43 +00:00
|
|
|
|
//exec.Result.ErrorReason // {4}
|
2017-01-18 20:52:42 +00:00
|
|
|
|
);
|
2017-01-07 21:31:42 +00:00
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
|
|
|
|
private async Task MessageReceivedHandler(SocketMessage msg)
|
2017-01-07 21:31:42 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
try
|
2017-01-07 21:31:42 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
if (msg.Author.IsBot || !_bot.Ready.Task.IsCompleted) //no bots, wait until bot connected and initialized
|
2017-05-29 04:13:22 +00:00
|
|
|
|
return;
|
2017-01-06 20:56:47 +00:00
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
if (!(msg is SocketUserMessage usrMsg))
|
|
|
|
|
return;
|
2017-01-22 20:06:10 +00:00
|
|
|
|
#if !GLOBAL_NADEKO
|
2017-05-29 04:13:22 +00:00
|
|
|
|
// track how many messagges each user is sending
|
|
|
|
|
UserMessagesSent.AddOrUpdate(usrMsg.Author.Id, 1, (key, old) => ++old);
|
2017-01-22 20:06:10 +00:00
|
|
|
|
#endif
|
2017-01-07 21:31:42 +00:00
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
var channel = msg.Channel as ISocketMessageChannel;
|
|
|
|
|
var guild = (msg.Channel as SocketTextChannel)?.Guild;
|
2017-01-07 21:31:42 +00:00
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
await TryRunCommand(guild, channel, usrMsg);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_log.Warn("Error in CommandHandler");
|
|
|
|
|
_log.Warn(ex);
|
|
|
|
|
if (ex.InnerException != null)
|
2017-03-31 12:07:18 +00:00
|
|
|
|
{
|
2017-05-29 04:13:22 +00:00
|
|
|
|
_log.Warn("Inner Exception of the error in CommandHandler");
|
|
|
|
|
_log.Warn(ex.InnerException);
|
2017-03-31 12:07:18 +00:00
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
2017-03-31 12:07:18 +00:00
|
|
|
|
}
|
2017-02-02 13:16:28 +00:00
|
|
|
|
|
2017-05-29 04:13:22 +00:00
|
|
|
|
public async Task TryRunCommand(SocketGuild guild, ISocketMessageChannel channel, IUserMessage usrMsg)
|
2017-03-31 12:07:18 +00:00
|
|
|
|
{
|
|
|
|
|
var execTime = Environment.TickCount;
|
2016-11-05 19:03:50 +00:00
|
|
|
|
|
2017-05-29 23:53:16 +00:00
|
|
|
|
//its nice to have early blockers and early blocking executors separate, but
|
|
|
|
|
//i could also have one interface with priorities, and just put early blockers on
|
|
|
|
|
//highest priority. :thinking:
|
2017-10-30 13:38:50 +00:00
|
|
|
|
foreach (var blocker in _earlyBlockers)
|
2017-05-25 02:24:43 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
if (await blocker.TryBlockEarly(guild, usrMsg).ConfigureAwait(false))
|
2017-05-25 02:24:43 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
_log.Info("Blocked User: [{0}] Message: [{1}] Service: [{2}]", usrMsg.Author, usrMsg.Content, blocker.GetType().Name);
|
2017-05-25 02:24:43 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-29 23:53:16 +00:00
|
|
|
|
var exec2 = Environment.TickCount - execTime;
|
2017-03-31 12:07:18 +00:00
|
|
|
|
|
2017-10-30 13:38:50 +00:00
|
|
|
|
foreach (var exec in _earlyBlockingExecutors)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
if (await exec.TryExecuteEarly(_client, guild, usrMsg).ConfigureAwait(false))
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
_log.Info("User [{0}] executed [{1}] in [{2}]", usrMsg.Author, usrMsg.Content, exec.GetType().Name);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-01-07 21:31:42 +00:00
|
|
|
|
|
2017-03-31 12:07:18 +00:00
|
|
|
|
var exec3 = Environment.TickCount - execTime;
|
2017-02-05 18:15:05 +00:00
|
|
|
|
|
2017-03-31 12:07:18 +00:00
|
|
|
|
string messageContent = usrMsg.Content;
|
2017-10-30 13:38:50 +00:00
|
|
|
|
foreach (var exec in _inputTransformers)
|
2017-05-29 23:53:16 +00:00
|
|
|
|
{
|
|
|
|
|
string newContent;
|
2017-10-30 13:38:50 +00:00
|
|
|
|
if ((newContent = await exec.TransformInput(guild, usrMsg.Channel, usrMsg.Author, messageContent).ConfigureAwait(false)) != messageContent.ToLowerInvariant())
|
2017-05-29 23:53:16 +00:00
|
|
|
|
{
|
|
|
|
|
messageContent = newContent;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-05 22:46:58 +00:00
|
|
|
|
var prefix = GetPrefix(guild?.Id);
|
2017-08-22 03:47:57 +00:00
|
|
|
|
var isPrefixCommand = messageContent.StartsWith(".prefix");
|
2017-03-31 12:07:18 +00:00
|
|
|
|
// execute the command and measure the time it took
|
2017-07-25 16:31:30 +00:00
|
|
|
|
if (messageContent.StartsWith(prefix) || isPrefixCommand)
|
2017-03-31 12:07:18 +00:00
|
|
|
|
{
|
2017-07-25 16:31:30 +00:00
|
|
|
|
var result = await ExecuteCommandAsync(new CommandContext(_client, usrMsg), messageContent, isPrefixCommand ? 1 : prefix.Length, _services, MultiMatchHandling.Best);
|
2017-05-22 23:59:31 +00:00
|
|
|
|
execTime = Environment.TickCount - execTime;
|
2017-08-22 03:47:57 +00:00
|
|
|
|
|
2017-05-30 04:54:59 +00:00
|
|
|
|
if (result.Success)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-05-30 04:54:59 +00:00
|
|
|
|
await LogSuccessfulExecution(usrMsg, channel as ITextChannel, exec2, exec3, execTime).ConfigureAwait(false);
|
2017-06-07 06:34:03 +00:00
|
|
|
|
await CommandExecuted(usrMsg, result.Info).ConfigureAwait(false);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-30 04:54:59 +00:00
|
|
|
|
else if (result.Error != null)
|
|
|
|
|
{
|
2017-08-22 03:47:57 +00:00
|
|
|
|
LogErroredExecution(result.Error, usrMsg, channel as ITextChannel, exec2, exec3, execTime);
|
2017-06-12 12:26:14 +00:00
|
|
|
|
if (guild != null)
|
|
|
|
|
await CommandErrored(result.Info, channel as ITextChannel, result.Error);
|
2017-05-30 04:54:59 +00:00
|
|
|
|
}
|
2017-08-22 03:47:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
await OnMessageNoTrigger(usrMsg).ConfigureAwait(false);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
2017-03-31 12:07:18 +00:00
|
|
|
|
|
2017-10-30 13:38:50 +00:00
|
|
|
|
foreach (var exec in _lateExecutors)
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
await exec.LateExecute(_client, guild, usrMsg).ConfigureAwait(false);
|
2017-03-31 12:07:18 +00:00
|
|
|
|
}
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2016-08-29 22:47:04 +00:00
|
|
|
|
}
|
2017-01-07 21:31:42 +00:00
|
|
|
|
|
2017-06-07 06:34:03 +00:00
|
|
|
|
public Task<(bool Success, string Error, CommandInfo Info)> ExecuteCommandAsync(CommandContext context, string input, int argPos, IServiceProvider serviceProvider, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
2017-05-29 23:53:16 +00:00
|
|
|
|
=> ExecuteCommand(context, input.Substring(argPos), serviceProvider, multiMatchHandling);
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
2016-12-21 08:33:47 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
public async Task<(bool Success, string Error, CommandInfo Info)> ExecuteCommand(CommandContext context, string input, IServiceProvider services, MultiMatchHandling multiMatchHandling = MultiMatchHandling.Exception)
|
2016-12-18 00:52:53 +00:00
|
|
|
|
{
|
|
|
|
|
var searchResult = _commandService.Search(context, input);
|
2016-09-22 18:53:49 +00:00
|
|
|
|
if (!searchResult.IsSuccess)
|
2017-06-07 06:34:03 +00:00
|
|
|
|
return (false, null, null);
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
|
|
|
|
var commands = searchResult.Commands;
|
2017-07-15 03:04:16 +00:00
|
|
|
|
var preconditionResults = new Dictionary<CommandMatch, PreconditionResult>();
|
|
|
|
|
|
|
|
|
|
foreach (var match in commands)
|
2016-09-22 18:53:49 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
preconditionResults[match] = await match.Command.CheckPreconditionsAsync(context, services).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var successfulPreconditions = preconditionResults
|
|
|
|
|
.Where(x => x.Value.IsSuccess)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
|
|
|
|
if (successfulPreconditions.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
//All preconditions failed, return the one from the highest priority command
|
|
|
|
|
var bestCandidate = preconditionResults
|
|
|
|
|
.OrderByDescending(x => x.Key.Command.Priority)
|
|
|
|
|
.FirstOrDefault(x => !x.Value.IsSuccess);
|
|
|
|
|
return (false, bestCandidate.Value.ErrorReason, commands[0].Command);
|
|
|
|
|
}
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
var parseResultsDict = new Dictionary<CommandMatch, ParseResult>();
|
|
|
|
|
foreach (var pair in successfulPreconditions)
|
|
|
|
|
{
|
|
|
|
|
var parseResult = await pair.Key.ParseAsync(context, searchResult, pair.Value, services).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (parseResult.Error == CommandError.MultipleMatches)
|
2016-09-22 18:53:49 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
IReadOnlyList<TypeReaderValue> argList, paramList;
|
|
|
|
|
switch (multiMatchHandling)
|
2016-09-22 18:53:49 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
case MultiMatchHandling.Best:
|
|
|
|
|
argList = parseResult.ArgValues.Select(x => x.Values.OrderByDescending(y => y.Score).First()).ToImmutableArray();
|
|
|
|
|
paramList = parseResult.ParamValues.Select(x => x.Values.OrderByDescending(y => y.Score).First()).ToImmutableArray();
|
|
|
|
|
parseResult = ParseResult.FromSuccess(argList, paramList);
|
|
|
|
|
break;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
2017-07-15 03:04:16 +00:00
|
|
|
|
}
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
parseResultsDict[pair.Key] = parseResult;
|
|
|
|
|
}
|
|
|
|
|
// Calculates the 'score' of a command given a parse result
|
|
|
|
|
float CalculateScore(CommandMatch match, ParseResult parseResult)
|
|
|
|
|
{
|
|
|
|
|
float argValuesScore = 0, paramValuesScore = 0;
|
|
|
|
|
|
|
|
|
|
if (match.Command.Parameters.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
var argValuesSum = parseResult.ArgValues?.Sum(x => x.Values.OrderByDescending(y => y.Score).FirstOrDefault().Score) ?? 0;
|
|
|
|
|
var paramValuesSum = parseResult.ParamValues?.Sum(x => x.Values.OrderByDescending(y => y.Score).FirstOrDefault().Score) ?? 0;
|
|
|
|
|
|
|
|
|
|
argValuesScore = argValuesSum / match.Command.Parameters.Count;
|
|
|
|
|
paramValuesScore = paramValuesSum / match.Command.Parameters.Count;
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
2016-10-14 22:03:52 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
var totalArgsScore = (argValuesScore + paramValuesScore) / 2;
|
|
|
|
|
return match.Command.Priority + totalArgsScore * 0.99f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Order the parse results by their score so that we choose the most likely result to execute
|
|
|
|
|
var parseResults = parseResultsDict
|
|
|
|
|
.OrderByDescending(x => CalculateScore(x.Key, x.Value));
|
2016-09-22 18:53:49 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
var successfulParses = parseResults
|
|
|
|
|
.Where(x => x.Value.IsSuccess)
|
|
|
|
|
.ToArray();
|
2017-05-29 04:13:22 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
if (successfulParses.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
//All parses failed, return the one from the highest priority command, using score as a tie breaker
|
|
|
|
|
var bestMatch = parseResults
|
|
|
|
|
.FirstOrDefault(x => !x.Value.IsSuccess);
|
|
|
|
|
return (false, bestMatch.Value.ErrorReason, commands[0].Command);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cmd = successfulParses[0].Key.Command;
|
|
|
|
|
|
|
|
|
|
// Bot will ignore commands which are ran more often than what specified by
|
|
|
|
|
// GlobalCommandsCooldown constant (miliseconds)
|
|
|
|
|
if (!UsersOnShortCooldown.Add(context.Message.Author.Id))
|
|
|
|
|
return (false, null, cmd);
|
|
|
|
|
//return SearchResult.FromError(CommandError.Exception, "You are on a global cooldown.");
|
|
|
|
|
|
|
|
|
|
var commandName = cmd.Aliases.First();
|
2017-10-30 13:38:50 +00:00
|
|
|
|
foreach (var exec in _lateBlockers)
|
2017-07-15 03:04:16 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
if (await exec.TryBlockLate(_client, context.Message, context.Guild, context.Channel, context.User, cmd.Module.GetTopLevelModule().Name, commandName).ConfigureAwait(false))
|
2017-05-29 04:13:22 +00:00
|
|
|
|
{
|
2017-10-30 13:38:50 +00:00
|
|
|
|
_log.Info("Late blocking User [{0}] Command: [{1}] in [{2}]", context.User, commandName, exec.GetType().Name);
|
2017-07-15 03:04:16 +00:00
|
|
|
|
return (false, null, cmd);
|
2017-05-29 04:13:22 +00:00
|
|
|
|
}
|
2017-07-15 03:04:16 +00:00
|
|
|
|
}
|
2016-10-07 22:23:41 +00:00
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
//If we get this far, at least one parse was successful. Execute the most likely overload.
|
|
|
|
|
var chosenOverload = successfulParses[0];
|
|
|
|
|
var execResult = (ExecuteResult)await chosenOverload.Key.ExecuteAsync(context, chosenOverload.Value, services).ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (execResult.Exception != null && (!(execResult.Exception is HttpException he) || he.DiscordCode != 50013))
|
|
|
|
|
{
|
|
|
|
|
lock (errorLogLock)
|
2017-05-30 05:03:35 +00:00
|
|
|
|
{
|
2017-07-15 03:04:16 +00:00
|
|
|
|
var now = DateTime.Now;
|
|
|
|
|
File.AppendAllText($"./command_errors_{now:yyyy-MM-dd}.txt",
|
|
|
|
|
$"[{now:HH:mm-yyyy-MM-dd}]" + Environment.NewLine
|
|
|
|
|
+ execResult.Exception.ToString() + Environment.NewLine
|
|
|
|
|
+ "------" + Environment.NewLine);
|
|
|
|
|
_log.Warn(execResult.Exception);
|
2017-05-30 05:03:35 +00:00
|
|
|
|
}
|
2016-09-22 18:53:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-15 03:04:16 +00:00
|
|
|
|
return (true, null, cmd);
|
2016-12-21 08:33:47 +00:00
|
|
|
|
}
|
2017-06-01 03:12:00 +00:00
|
|
|
|
|
|
|
|
|
private readonly object errorLogLock = new object();
|
2016-08-29 22:47:04 +00:00
|
|
|
|
}
|
2016-12-31 12:21:18 +00:00
|
|
|
|
}
|