From 41101947b271889a68a32663de02ed82038d17e5 Mon Sep 17 00:00:00 2001 From: Pingex Date: Mon, 18 Apr 2016 12:24:28 +0200 Subject: [PATCH] Help prompt + command arguments. --- .../pingex/discordbot/CommandDispatcher.java | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/main/java/net/pingex/discordbot/CommandDispatcher.java b/src/main/java/net/pingex/discordbot/CommandDispatcher.java index b470e83..85d8e29 100644 --- a/src/main/java/net/pingex/discordbot/CommandDispatcher.java +++ b/src/main/java/net/pingex/discordbot/CommandDispatcher.java @@ -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 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