pingex
/
DiscordBot
Archived
1
0
Fork 0

Command shorthand, ie `!hello:world` => `!hw`.

master
Pingex aka Raphaël 9 years ago
parent 2a7b245bc0
commit 94da2ceb2d

@ -18,4 +18,9 @@ public @interface Command
* Minimum number of arguments expected * Minimum number of arguments expected
*/ */
int minArgs() default 0; int minArgs() default 0;
/**
* Shortened command
*/
String shorthand() default "";
} }

@ -27,6 +27,11 @@ public class CommandDispatcher
*/ */
private HashMap<String, InvokableMethod> commandList; private HashMap<String, InvokableMethod> commandList;
/**
* Contains all the shorthanded commands
*/
private HashMap<String, String> shortList;
/** /**
* Basic Constructor, automatically rebuilds command list from the modules registry * Basic Constructor, automatically rebuilds command list from the modules registry
*/ */
@ -43,24 +48,26 @@ public class CommandDispatcher
@EventSubscriber @EventSubscriber
public void onMessageReceivedEvent(MessageReceivedEvent event) public void onMessageReceivedEvent(MessageReceivedEvent event)
{ {
Matcher m = Pattern.compile("^!(\\w+):(\\w+)(?: (.*))?$").matcher(event.getMessage().getContent()); // TODO: Custom command prefix // Command matcher
Matcher m = Pattern.compile("^!([:\\w]+)(?: (.*))?$").matcher(event.getMessage().getContent());
if(!m.matches()) return; // We don't need to go further if it's not even a command if(!m.matches()) return; // We don't need to go further if it's not even a command
String fullCommand = m.group(1);
String module = m.group(1);
String command = m.group(2);
String fullCommand = module + ":" + command;
// Arg splitter // Arg splitter
Matcher am = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher((m.group(3) != null) ? m.group(3) : ""); Matcher am = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher((m.group(2) != null) ? m.group(2) : "");
ArrayList<String> args = new ArrayList<>(); ArrayList<String> args = new ArrayList<>();
while(am.find()) args.add(am.group(1).replace("\"", "")); while(am.find()) args.add(am.group(1).replace("\"", ""));
logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand); // Shorthand matcher
if(!fullCommand.contains(":") && shortList.containsKey(fullCommand))
fullCommand = shortList.get(fullCommand);
try try
{ {
if(commandList.containsKey(fullCommand)) if(commandList.containsKey(fullCommand))
{ {
logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand);
InvokableMethod foundMethod = commandList.get(fullCommand); InvokableMethod foundMethod = commandList.get(fullCommand);
Object[] parsedArray = new Object[foundMethod.getMethod().getParameterCount()]; Object[] parsedArray = new Object[foundMethod.getMethod().getParameterCount()];
parsedArray[0] = event; parsedArray[0] = event;
@ -108,6 +115,7 @@ public class CommandDispatcher
ArrayList<AbstractModule> registry = ModulesRegistry.getRegistry(); ArrayList<AbstractModule> registry = ModulesRegistry.getRegistry();
commandList = new HashMap<>(); commandList = new HashMap<>();
shortList = new HashMap<>();
for(AbstractModule i : registry) for(AbstractModule i : registry)
if(i.getClass().isAnnotationPresent(Controllable.class)) if(i.getClass().isAnnotationPresent(Controllable.class))
@ -118,7 +126,19 @@ public class CommandDispatcher
logger.info("Found " + id); logger.info("Found " + id);
if(j.getParameterCount() >= 1 && j.getParameterTypes()[0] == MessageReceivedEvent.class && j.getReturnType() == String.class) if(j.getParameterCount() >= 1 && j.getParameterTypes()[0] == MessageReceivedEvent.class && j.getReturnType() == String.class)
commandList.put(id, new InvokableMethod(j, i)); if(!commandList.containsKey(id))
{
commandList.put(id, new InvokableMethod(j, i));
// Eventual command shorthands
if(!j.getAnnotation(Command.class).shorthand().isEmpty())
if(!shortList.containsKey(j.getAnnotation(Command.class).shorthand()))
shortList.put(j.getAnnotation(Command.class).shorthand(), id);
else
logger.warning("Conflicting shorthand for command " + id);
}
else
logger.warning("Conflicting command " + id);
else else
logger.warning("Command [" + id + "]: incorrect function prototype, thus won't be added to the command list."); logger.warning("Command [" + id + "]: incorrect function prototype, thus won't be added to the command list.");
} }