Cleaner handling of commands, less anonymous Tuples
This commit is contained in:
parent
8fab315cc6
commit
51db31cb72
|
@ -53,7 +53,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var req = await CommandRequest.Create(message);
|
var req = await CommandRequest.Create(message);
|
||||||
var resultCode = req.Item2;
|
var resultCode = req.Response;
|
||||||
switch (resultCode)
|
switch (resultCode)
|
||||||
{
|
{
|
||||||
case CommandRequest.RequestCode.Invalid:
|
case CommandRequest.RequestCode.Invalid:
|
||||||
|
@ -67,7 +67,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
|
||||||
$"Unauthorized user tried to run command: {message.Author.Username} -> {message.Content}");
|
$"Unauthorized user tried to run command: {message.Author.Username} -> {message.Content}");
|
||||||
break;
|
break;
|
||||||
case CommandRequest.RequestCode.OK:
|
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(
|
await Logger.Main.Log(
|
||||||
$"User is trying to use blocked command in PM: {message.Author.Username}");
|
$"User is trying to use blocked command in PM: {message.Author.Username}");
|
||||||
|
@ -76,7 +76,7 @@ namespace DeukBot4.MessageHandlers.CommandHandler
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await req.Item1.Command.Invoke(req.Item1);
|
await req.Request.Command.Invoke(req.Request);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
@ -85,9 +85,9 @@ namespace DeukBot4.MessageHandlers.CommandHandler
|
||||||
break;
|
break;
|
||||||
case CommandRequest.RequestCode.UnknownCommand:
|
case CommandRequest.RequestCode.UnknownCommand:
|
||||||
var permission = await PermissionValidator.GetUserPermissionLevel(message);
|
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(
|
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");
|
$"Alternatively, use ``{CommandTrigger}help`` for a list of all commands");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -48,20 +48,34 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
|
||||||
return OriginalMessage.Channel.SendMessageAsync("", embed: eb.Build());
|
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 originalMessage = message;
|
||||||
var content = message.Content;
|
var content = message.Content;
|
||||||
var res = CommandNameMatcher.Match(content);
|
var res = CommandNameMatcher.Match(content);
|
||||||
if (res.Groups.Count <= 2)
|
if (res.Groups.Count <= 2)
|
||||||
return (null, RequestCode.Invalid, null);
|
return new CommandRequestResponse(null, RequestCode.Invalid, null);
|
||||||
|
|
||||||
var commandName = res.Groups[1].Value;
|
var commandName = res.Groups[1].Value;
|
||||||
var command = CommandHandler.GetCommand(commandName);
|
var command = CommandHandler.GetCommand(commandName);
|
||||||
if (command == null)
|
if (command == null)
|
||||||
{
|
{
|
||||||
return (null, RequestCode.UnknownCommand, commandName);
|
return new CommandRequestResponse(null, RequestCode.UnknownCommand, commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
PermissionLevel permission;
|
PermissionLevel permission;
|
||||||
|
@ -72,19 +86,21 @@ namespace DeukBot4.MessageHandlers.CommandHandler.RequestStructure
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
await Logger.Main.LogError(e.Message);
|
await Logger.Main.LogError(e.Message);
|
||||||
return (null, RequestCode.Forbidden, null);
|
return new CommandRequestResponse(null, RequestCode.Forbidden, null);
|
||||||
}
|
}
|
||||||
if (permission < command.Permission)
|
if (permission < command.Permission)
|
||||||
{
|
{
|
||||||
return (null, RequestCode.Forbidden, permission);
|
return new CommandRequestResponse(null, RequestCode.Forbidden, permission.ToString());
|
||||||
}
|
}
|
||||||
var parameterString = res.Groups[2].Value;
|
var parameterString = res.Groups[2].Value;
|
||||||
var parameters = ParameterMatcher.GetParameterValues(command, parameterString);
|
var parameters = ParameterMatcher.GetParameterValues(command, parameterString);
|
||||||
if (parameters == null)
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,6 +76,10 @@ namespace DeukBot4
|
||||||
}
|
}
|
||||||
BotId = Client.CurrentUser.Id;
|
BotId = Client.CurrentUser.Id;
|
||||||
IsConnected = true;
|
IsConnected = true;
|
||||||
|
|
||||||
|
var channel = (ITextChannel)Client.GetChannel(169864553801318400);
|
||||||
|
var message = await channel.GetMessageAsync(499974464197623828);
|
||||||
|
Console.WriteLine(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue