Cleaner handling of commands, less anonymous Tuples

This commit is contained in:
Deukhoofd 2018-10-11 20:35:34 +02:00
parent 8fab315cc6
commit 51db31cb72
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 32 additions and 12 deletions

View File

@ -53,7 +53,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
return;
var req = await CommandRequest.Create(message);
var resultCode = req.Item2;
var resultCode = req.Response;
switch (resultCode)
{
case CommandRequest.RequestCode.Invalid:
@ -67,7 +67,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
$"Unauthorized user tried to run command: {message.Author.Username} -> {message.Content}");
break;
case CommandRequest.RequestCode.OK:
if (!(message.Channel is IGuildChannel) && req.Item1.Command.ForbidInPm)
if (!(message.Channel is IGuildChannel) && req.Request.Command.ForbidInPm)
{
await Logger.Main.Log(
$"User is trying to use blocked command in PM: {message.Author.Username}");
@ -76,7 +76,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
try
{
await req.Item1.Command.Invoke(req.Item1);
await req.Request.Command.Invoke(req.Request);
}
catch (Exception e)
{
@ -85,9 +85,9 @@ namespace DeukBot4.MessageHandlers.CommandHandler
break;
case CommandRequest.RequestCode.UnknownCommand:
var permission = await PermissionValidator.GetUserPermissionLevel(message);
var similar = await GetSimilarCommand(req.Item3.ToString(), permission);
var similar = await GetSimilarCommand(req.CommandName, permission);
await message.Channel.SendMessageAsync(
$"Unknown command: ``{req.Item3.ToString()}``. Did you mean: ``{similar}``? " +
$"Unknown command: ``{req.CommandName}``. Did you mean: ``{similar}``? " +
$"Alternatively, use ``{CommandTrigger}help`` for a list of all commands");
break;
default:

View File

@ -48,20 +48,34 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
return OriginalMessage.Channel.SendMessageAsync("", embed: eb.Build());
}
public class CommandRequestResponse
{
public CommandRequest Request { get; }
public RequestCode Response { get; }
public string CommandName { get; }
public static async Task<(CommandRequest, RequestCode, object)> Create(SocketMessage message)
public CommandRequestResponse(CommandRequest request, RequestCode response, string commandName)
{
Request = request;
Response = response;
CommandName = commandName;
}
}
public static async Task<CommandRequestResponse> Create(SocketMessage message)
{
var originalMessage = message;
var content = message.Content;
var res = CommandNameMatcher.Match(content);
if (res.Groups.Count <= 2)
return (null, RequestCode.Invalid, null);
return new CommandRequestResponse(null, RequestCode.Invalid, null);
var commandName = res.Groups[1].Value;
var command = CommandHandler.GetCommand(commandName);
if (command == null)
{
return (null, RequestCode.UnknownCommand, commandName);
return new CommandRequestResponse(null, RequestCode.UnknownCommand, commandName);
}
PermissionLevel permission;
@ -72,19 +86,21 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
catch (Exception e)
{
await Logger.Main.LogError(e.Message);
return (null, RequestCode.Forbidden, null);
return new CommandRequestResponse(null, RequestCode.Forbidden, null);
}
if (permission < command.Permission)
{
return (null, RequestCode.Forbidden, permission);
return new CommandRequestResponse(null, RequestCode.Forbidden, permission.ToString());
}
var parameterString = res.Groups[2].Value;
var parameters = ParameterMatcher.GetParameterValues(command, parameterString);
if (parameters == null)
{
return (null, RequestCode.InvalidParameters, parameterString);
return new CommandRequestResponse(null, RequestCode.InvalidParameters, parameterString);
}
return (new CommandRequest(originalMessage, command, permission, parameters), RequestCode.OK, null);
return new CommandRequestResponse(new CommandRequest(originalMessage, command, permission, parameters),
RequestCode.OK, null);
}
}

View File

@ -76,6 +76,10 @@ namespace DeukBot4
}
BotId = Client.CurrentUser.Id;
IsConnected = true;
var channel = (ITextChannel)Client.GetChannel(169864553801318400);
var message = await channel.GetMessageAsync(499974464197623828);
Console.WriteLine(message);
}
}
}