ICommandExecutor now uses a Context instead of a MRE+Arguments.

master
Pingex aka Raphaël 9 years ago
parent a38e9e7ce7
commit 34af04a7b0

@ -164,27 +164,9 @@ public abstract class Command implements ICommandExecutor
return new Command(name, aliases, description, isEnabled, usage, commandOptions) return new Command(name, aliases, description, isEnabled, usage, commandOptions)
{ {
@Override @Override
public void execute(MessageReceivedEvent event, List<String> arguments) throws Throwable public void execute(Context context) throws Throwable
{ {
toExecute.execute(event, arguments); toExecute.execute(context);
}
};
}
/**
* Build a new Command using a method and a target object (for use with annotated command)
* @param target The method which needs to be invoked
* @param invokable The matching object
* @return Built command.
*/
public Command build(Method target, Object invokable)
{
return new Command(name, aliases, description, isEnabled, usage, commandOptions)
{
@Override
public void execute(MessageReceivedEvent event, List<String> arguments) throws Throwable
{
target.invoke(invokable, event, arguments);
} }
}; };
} }

@ -67,8 +67,10 @@ public class CommandHandler
return; return;
} }
final Context invocationContext = new Context(targetCommand.get(), arguments, event.getMessage(), event.getClient());
// Audit // Audit
AuditResult result = AuditManager.doAudit(new Context(targetCommand.get(), arguments, event.getMessage(), event.getClient())); AuditResult result = AuditManager.doAudit(invocationContext);
if(result.getOpcode().equals(AuditResult.ResultCode.FAIL)) if(result.getOpcode().equals(AuditResult.ResultCode.FAIL))
{ {
LOGGER.info("Denied command {} for user #{}. OPCode: {}, Reason: {}", targetCommand.get().getName(), event.getMessage().getAuthor().getID(), result.getOpcode(), result.getMessage()); LOGGER.info("Denied command {} for user #{}. OPCode: {}, Reason: {}", targetCommand.get().getName(), event.getMessage().getAuthor().getID(), result.getOpcode(), result.getMessage());
@ -85,7 +87,7 @@ public class CommandHandler
{ {
try try
{ {
targetCommand.get().execute(event, arguments); targetCommand.get().execute(invocationContext);
} }
catch(Throwable throwable) catch(Throwable throwable)
{ {

@ -1,13 +1,10 @@
package net.pingex.dcf.commands; package net.pingex.dcf.commands;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
import java.util.List;
/** /**
* The body of a command. * The body of a command.
*/ */
@FunctionalInterface @FunctionalInterface
public interface ICommandExecutor public interface ICommandExecutor
{ {
void execute(MessageReceivedEvent event, List<String> arguments) throws Throwable; void execute(Context context) throws Throwable;
} }

@ -45,7 +45,7 @@ public class InternalCommands implements IWithCommands
} }
@Override @Override
public void execute(MessageReceivedEvent event, List<String> arguments) public void execute(Context context)
{ {
// Parameters // Parameters
Set<Command> bank = CommandRegistry.getRegistry(); Set<Command> bank = CommandRegistry.getRegistry();
@ -58,19 +58,19 @@ public class InternalCommands implements IWithCommands
// Parsing // Parsing
try try
{ {
List<Object> output = ArgumentParser.parseAll(Collections.singletonList(Integer.class), arguments); List<Object> output = ArgumentParser.parseAll(Collections.singletonList(Integer.class), context.getArguments());
requestedPage = output.get(0) != null ? (int) output.get(0) : 1; requestedPage = output.get(0) != null ? (int) output.get(0) : 1;
} }
catch(ArgumentParser.ParserException e) catch(ArgumentParser.ParserException e)
{ {
DiscordInteractionsUtil.sendMessage(event.getMessage().getChannel(), e.getMessage()); DiscordInteractionsUtil.sendMessage(context.getChannel(), e.getMessage());
return; return;
} }
// Checks // Checks
if(requestedPage <= 0 || requestedPage > amountPages) if(requestedPage <= 0 || requestedPage > amountPages)
{ {
DiscordInteractionsUtil.sendMessage(event.getMessage().getChannel(), "Requested page is invalid. Number of available pages: " + amountPages); DiscordInteractionsUtil.sendMessage(context.getChannel(), "Requested page is invalid. Number of available pages: " + amountPages);
return; return;
} }
@ -96,7 +96,7 @@ public class InternalCommands implements IWithCommands
} }
output.append("```"); output.append("```");
DiscordInteractionsUtil.sendMessage(event.getMessage().getChannel(), output.toString()); DiscordInteractionsUtil.sendMessage(context.getChannel(), output.toString());
} }
} }
@ -120,19 +120,19 @@ public class InternalCommands implements IWithCommands
} }
@Override @Override
public void execute(MessageReceivedEvent event, List<String> arguments) throws Throwable public void execute(Context context) throws Throwable
{ {
// Checks // Checks
if(arguments.size() != 1) // Arg check if(context.getArguments().size() != 1) // Arg check
{ {
DiscordInteractionsUtil.sendMessage(event.getMessage().getChannel(), "Invalid argument."); DiscordInteractionsUtil.sendMessage(context.getChannel(), "Invalid argument.");
return; return;
} }
Optional<Command> uncheckedTarget = CommandRegistry.getCommandOrAliasByName(arguments.get(0)); Optional<Command> uncheckedTarget = CommandRegistry.getCommandOrAliasByName(context.getArguments().get(0));
if(!uncheckedTarget.isPresent()) // Command existence if(!uncheckedTarget.isPresent()) // Command existence
{ {
DiscordInteractionsUtil.sendMessage(event.getMessage().getChannel(), "Target command not found."); DiscordInteractionsUtil.sendMessage(context.getChannel(), "Target command not found.");
return; return;
} }
@ -151,7 +151,7 @@ public class InternalCommands implements IWithCommands
output.append("Enabled: ").append(target.isEnabled() ? "Yes" : "No").append("\n"); output.append("Enabled: ").append(target.isEnabled() ? "Yes" : "No").append("\n");
output.append("```"); output.append("```");
DiscordInteractionsUtil.sendMessage(event.getMessage().getChannel(), output.toString()); DiscordInteractionsUtil.sendMessage(context.getChannel(), output.toString());
} }
} }
} }