2018-03-28 23:34:48 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
|
|
|
|
|
using DeukBot4.MessageHandlers.Permissions;
|
|
|
|
|
using DeukBot4.Utilities;
|
2018-03-29 19:57:16 +00:00
|
|
|
|
using Discord;
|
2018-03-28 23:34:48 +00:00
|
|
|
|
|
|
|
|
|
namespace DeukBot4.MessageHandlers.CommandHandler
|
|
|
|
|
{
|
|
|
|
|
public static class CommandHandler
|
|
|
|
|
{
|
2018-03-30 12:51:38 +00:00
|
|
|
|
public static Dictionary<string, Command> Commands { get; } = new Dictionary<string, Command>();
|
2018-03-30 13:41:42 +00:00
|
|
|
|
public const char CommandTrigger = '!';
|
2018-03-28 23:34:48 +00:00
|
|
|
|
|
|
|
|
|
public static void Build()
|
|
|
|
|
{
|
|
|
|
|
var commandContainers = typeof(CommandHandler).Assembly.GetTypes()
|
|
|
|
|
.Where(x => typeof(CommandContainerBase).IsAssignableFrom(x) && !x.IsAbstract);
|
|
|
|
|
|
|
|
|
|
foreach (var commandContainer in commandContainers)
|
|
|
|
|
{
|
|
|
|
|
if (!(Activator.CreateInstance(commandContainer) is CommandContainerBase obj))
|
|
|
|
|
continue;
|
|
|
|
|
var commands = obj.GetCommands();
|
|
|
|
|
foreach (var command in commands)
|
|
|
|
|
{
|
|
|
|
|
Commands.Add(command.Name.ToLowerInvariant(), command);
|
2018-05-06 17:15:42 +00:00
|
|
|
|
foreach (var commandAlternative in command.Alternatives)
|
|
|
|
|
{
|
|
|
|
|
Commands.Add(commandAlternative.ToLowerInvariant(), command);
|
|
|
|
|
}
|
2018-03-28 23:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-13 16:41:59 +00:00
|
|
|
|
Logger.Main.Log(
|
2018-03-30 12:51:38 +00:00
|
|
|
|
$"Loaded following commands for container {obj.Name}: {commands.Select(x => x.Name).Join(", ")}");
|
2018-03-28 23:34:48 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-29 22:28:52 +00:00
|
|
|
|
public static async Task HandleMessage(ReceivedMessage receivedMessage)
|
2018-03-28 23:34:48 +00:00
|
|
|
|
{
|
2019-01-29 22:28:52 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(receivedMessage.Message.Content))
|
2018-03-29 17:38:23 +00:00
|
|
|
|
return;
|
|
|
|
|
|
2019-01-29 22:28:52 +00:00
|
|
|
|
if (receivedMessage.Message.Content[0] != CommandTrigger && receivedMessage.Message.MentionedUsers.All(x => x.Id != Program.BotId))
|
2018-03-30 12:51:38 +00:00
|
|
|
|
return;
|
2019-01-29 22:28:52 +00:00
|
|
|
|
if (receivedMessage.Message.Content[1] == '!')
|
2018-07-30 19:46:57 +00:00
|
|
|
|
return;
|
2018-03-30 12:51:38 +00:00
|
|
|
|
|
2019-01-29 22:28:52 +00:00
|
|
|
|
receivedMessage.IsHandled = true;
|
|
|
|
|
var message = receivedMessage.Message;
|
|
|
|
|
|
2018-03-29 13:35:00 +00:00
|
|
|
|
var req = await CommandRequest.Create(message);
|
2018-10-11 18:35:34 +00:00
|
|
|
|
var resultCode = req.Response;
|
2018-03-30 12:51:38 +00:00
|
|
|
|
switch (resultCode)
|
2018-03-28 23:34:48 +00:00
|
|
|
|
{
|
2018-03-30 12:51:38 +00:00
|
|
|
|
case CommandRequest.RequestCode.Invalid:
|
2018-08-13 16:41:59 +00:00
|
|
|
|
await Logger.Main.LogError("Invalid content: " + message.Content);
|
2018-03-30 12:51:38 +00:00
|
|
|
|
return;
|
2018-03-30 13:41:42 +00:00
|
|
|
|
case CommandRequest.RequestCode.InvalidParameters:
|
2018-08-13 16:41:59 +00:00
|
|
|
|
await Logger.Main.LogError("Invalid parameters: " + message.Content);
|
2018-03-30 13:41:42 +00:00
|
|
|
|
break;
|
2018-03-30 12:51:38 +00:00
|
|
|
|
case CommandRequest.RequestCode.Forbidden:
|
2018-08-13 16:41:59 +00:00
|
|
|
|
await Logger.Main.Log(
|
2018-03-30 12:51:38 +00:00
|
|
|
|
$"Unauthorized user tried to run command: {message.Author.Username} -> {message.Content}");
|
|
|
|
|
break;
|
|
|
|
|
case CommandRequest.RequestCode.OK:
|
2018-10-11 18:35:34 +00:00
|
|
|
|
if (!(message.Channel is IGuildChannel) && req.Request.Command.ForbidInPm)
|
2018-03-30 12:51:38 +00:00
|
|
|
|
{
|
2018-08-13 16:41:59 +00:00
|
|
|
|
await Logger.Main.Log(
|
2018-03-30 12:51:38 +00:00
|
|
|
|
$"User is trying to use blocked command in PM: {message.Author.Username}");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-08-12 11:11:20 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-10-11 18:35:34 +00:00
|
|
|
|
await req.Request.Command.Invoke(req.Request);
|
2018-08-12 11:11:20 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2018-08-13 16:41:59 +00:00
|
|
|
|
await Logger.Main.Log("An error occured: \n" + e);
|
2018-08-12 11:11:20 +00:00
|
|
|
|
}
|
2018-03-30 12:51:38 +00:00
|
|
|
|
break;
|
|
|
|
|
case CommandRequest.RequestCode.UnknownCommand:
|
2018-03-30 13:41:42 +00:00
|
|
|
|
var permission = await PermissionValidator.GetUserPermissionLevel(message);
|
2018-10-11 18:35:34 +00:00
|
|
|
|
var similar = await GetSimilarCommand(req.CommandName, permission);
|
2018-10-27 11:57:37 +00:00
|
|
|
|
var response = $"Unknown command: ``{req.CommandName}``. Did you mean: ``{similar}``? " +
|
|
|
|
|
$"Alternatively, use ``{CommandTrigger}help`` for a list of all commands";
|
|
|
|
|
var embed = EmbedFactory.GetStandardEmbedBuilder();
|
|
|
|
|
embed.Title = "Unknown Command";
|
|
|
|
|
embed.Description = response;
|
|
|
|
|
await message.Channel.SendMessageAsync("", embed: embed.Build());
|
2018-03-30 12:51:38 +00:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
2018-03-28 23:34:48 +00:00
|
|
|
|
}
|
2018-03-30 12:51:38 +00:00
|
|
|
|
}
|
2018-03-29 19:57:16 +00:00
|
|
|
|
|
2018-03-30 13:41:42 +00:00
|
|
|
|
private static async Task<string> GetSimilarCommand(string command, PermissionLevel permission)
|
2018-03-30 12:51:38 +00:00
|
|
|
|
{
|
|
|
|
|
var closestString = "";
|
|
|
|
|
var similarity = int.MaxValue;
|
|
|
|
|
foreach (var cmd in Commands)
|
2018-03-28 23:34:48 +00:00
|
|
|
|
{
|
2018-03-30 13:41:42 +00:00
|
|
|
|
if (cmd.Value.Permission > permission)
|
|
|
|
|
continue;
|
2018-03-30 12:51:38 +00:00
|
|
|
|
var distance = Lehvenstein.LevenshteinDistance(command, cmd.Key);
|
|
|
|
|
if (distance >= similarity)
|
|
|
|
|
continue;
|
|
|
|
|
similarity = distance;
|
|
|
|
|
closestString = cmd.Key;
|
2018-03-28 23:34:48 +00:00
|
|
|
|
}
|
2018-03-30 12:51:38 +00:00
|
|
|
|
return closestString;
|
2018-03-28 23:34:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Command GetCommand(string name)
|
|
|
|
|
{
|
|
|
|
|
return Commands.TryGetValue(name.ToLowerInvariant(), out var com) ? com : null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|