|
|
|
@ -49,7 +49,11 @@ public class CommandDispatcher
|
|
|
|
|
String module = m.group(1);
|
|
|
|
|
String command = m.group(2);
|
|
|
|
|
String fullCommand = module + ":" + command;
|
|
|
|
|
String[] args = (m.group(3) != null) ? m.group(3).split(" ") : new String[0];
|
|
|
|
|
|
|
|
|
|
// Arg splitter
|
|
|
|
|
Matcher am = Pattern.compile("([^\"]\\S*|\".+?\")\\s*").matcher((m.group(3) != null) ? m.group(3) : "");
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
@ -61,19 +65,19 @@ public class CommandDispatcher
|
|
|
|
|
Object[] parsedArray = new Object[foundMethod.getMethod().getParameterCount()];
|
|
|
|
|
parsedArray[0] = event;
|
|
|
|
|
|
|
|
|
|
if(foundMethod.getMethod().getParameterCount()-1 != args.length)
|
|
|
|
|
if(foundMethod.getMethod().getParameterCount()-1 != args.size())
|
|
|
|
|
{
|
|
|
|
|
event.getMessage().reply("Invalid arguments"); // TODO: Call !help
|
|
|
|
|
event.getMessage().reply("Invalid arguments. " + getHelp(fullCommand));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for(int i=1; i < foundMethod.getMethod().getParameterCount(); i++)
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
parsedArray[i] = parse(foundMethod.getMethod().getParameterTypes()[i], args[i-1]);
|
|
|
|
|
parsedArray[i] = parse(foundMethod.getMethod().getParameterTypes()[i], args.get(i-1));
|
|
|
|
|
} catch (IllegalArgumentException e)
|
|
|
|
|
{
|
|
|
|
|
event.getMessage().reply("Failed to parse arguments"); // TODO: Call !help
|
|
|
|
|
event.getMessage().reply("Failed to parse arguments, are they correct ? " + getHelp(fullCommand));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String answer = (String) commandList.get(fullCommand).invoke(parsedArray);
|
|
|
|
@ -83,10 +87,10 @@ public class CommandDispatcher
|
|
|
|
|
event.getMessage().reply("Unknown command");
|
|
|
|
|
} catch (MissingPermissionsException | HTTP429Exception | DiscordException e)
|
|
|
|
|
{
|
|
|
|
|
logger.warning("Couldn't reply to command: " + e.getMessage());
|
|
|
|
|
logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage());
|
|
|
|
|
} catch (IllegalArgumentException | IllegalAccessException | InvocationTargetException e)
|
|
|
|
|
{
|
|
|
|
|
logger.severe("Couldn't call target method: " + e.getMessage());
|
|
|
|
|
logger.severe("Couldn't call target method (" + e.getClass().getName() + "): " + e.getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -116,7 +120,7 @@ public class CommandDispatcher
|
|
|
|
|
if(j.getParameterCount() >= 1 && j.getParameterTypes()[0] == MessageReceivedEvent.class && j.getReturnType() == String.class)
|
|
|
|
|
commandList.put(id, new InvokableMethod(j, i));
|
|
|
|
|
else
|
|
|
|
|
logger.warning(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.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.info("... Done");
|
|
|
|
@ -142,4 +146,22 @@ public class CommandDispatcher
|
|
|
|
|
if(String.class == target) return value;
|
|
|
|
|
throw new IllegalArgumentException("Unknown target type");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Scans and print the usage of a command
|
|
|
|
|
* @param command Full command `module:command`
|
|
|
|
|
* @return Human-readable string
|
|
|
|
|
*/
|
|
|
|
|
private String getHelp(String command)
|
|
|
|
|
{
|
|
|
|
|
if(!commandList.containsKey(command)) return "No help available for this command.";
|
|
|
|
|
InvokableMethod matchingMethod = commandList.get(command);
|
|
|
|
|
|
|
|
|
|
StringBuffer toReturn = new StringBuffer("Usage: ");
|
|
|
|
|
toReturn.append("!" + command + " ");
|
|
|
|
|
for(int i=1; i<matchingMethod.getMethod().getParameterCount(); i++)
|
|
|
|
|
toReturn.append(matchingMethod.getMethod().getParameterTypes()[i].getName() + " ");
|
|
|
|
|
|
|
|
|
|
return toReturn.toString();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|