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
*/
int minArgs() default 0;
/**
* Shortened command
*/
String shorthand() default "";
}

@ -27,6 +27,11 @@ public class CommandDispatcher
*/
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
*/
@ -43,24 +48,26 @@ public class CommandDispatcher
@EventSubscriber
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
String module = m.group(1);
String command = m.group(2);
String fullCommand = module + ":" + command;
String fullCommand = m.group(1);
// 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<>();
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
{
if(commandList.containsKey(fullCommand))
{
logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand);
InvokableMethod foundMethod = commandList.get(fullCommand);
Object[] parsedArray = new Object[foundMethod.getMethod().getParameterCount()];
parsedArray[0] = event;
@ -108,6 +115,7 @@ public class CommandDispatcher
ArrayList<AbstractModule> registry = ModulesRegistry.getRegistry();
commandList = new HashMap<>();
shortList = new HashMap<>();
for(AbstractModule i : registry)
if(i.getClass().isAnnotationPresent(Controllable.class))
@ -118,7 +126,19 @@ public class CommandDispatcher
logger.info("Found " + id);
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
logger.warning("Command [" + id + "]: incorrect function prototype, thus won't be added to the command list.");
}