From 2d6e578feb22ff1b8d3fd21228d7c7c7dce350f4 Mon Sep 17 00:00:00 2001 From: Pingex Date: Sat, 7 May 2016 21:26:55 +0200 Subject: [PATCH] Rewritten /help command. --- .../pingex/discordbot/CommandDispatcher.java | 61 ++++++++----------- .../discordbot/InternalCommandsModule.java | 33 +++++++--- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/src/main/java/net/pingex/discordbot/CommandDispatcher.java b/src/main/java/net/pingex/discordbot/CommandDispatcher.java index 6ca2563..66d91d3 100644 --- a/src/main/java/net/pingex/discordbot/CommandDispatcher.java +++ b/src/main/java/net/pingex/discordbot/CommandDispatcher.java @@ -95,48 +95,41 @@ class CommandDispatcher Object[] parsedArray = null; // Conditions - try + if(commandList.containsKey(fullCommand)) { - if(commandList.containsKey(fullCommand)) - { - logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand); + logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand); - InvokableMethod foundMethod = commandList.get(fullCommand); - parsedArray = new Object[foundMethod.getMethod().getParameterCount()]; - parsedArray[0] = event; + InvokableMethod foundMethod = commandList.get(fullCommand); + parsedArray = new Object[foundMethod.getMethod().getParameterCount()]; + parsedArray[0] = event; - // To be redone, maybe ? - Boolean canOverrideRun = PermissionsModule.getInstance().canRun(event.getMessage().getGuild(), event.getMessage().getAuthor(), fullCommand); - if(!foundMethod.getMethod().getAnnotation(Command.class).permission().eval(event)) - commandAnswer = "Permission denied."; - if(canOverrideRun != null) - commandAnswer = canOverrideRun ? null : "Permission denied."; + // To be redone, maybe ? + Boolean canOverrideRun = PermissionsModule.getInstance().canRun(event.getMessage().getGuild(), event.getMessage().getAuthor(), fullCommand); + if(!foundMethod.getMethod().getAnnotation(Command.class).permission().eval(event)) + commandAnswer = "Permission denied."; + if(canOverrideRun != null) + commandAnswer = canOverrideRun ? null : "Permission denied."; - if(commandAnswer == null) + if(commandAnswer == null) + { + if(foundMethod.getMethod().getParameterCount()-1 == args.size()) { - if(foundMethod.getMethod().getParameterCount()-1 == args.size()) - { - for(int i=1; i < foundMethod.getMethod().getParameterCount(); i++) - try - { - parsedArray[i] = parse(foundMethod.getMethod().getParameterTypes()[i], args.get(i-1)); - } catch (IllegalArgumentException e) - { - commandAnswer = "Failed to parse arguments, are they correct ? " + commandList.get("internal:help").invoke(event, fullCommand); - break; - } - } - else - commandAnswer = "Invalid arguments. " + commandList.get("internal:help").invoke(event, fullCommand); + for(int i=1; i < foundMethod.getMethod().getParameterCount(); i++) + try + { + parsedArray[i] = parse(foundMethod.getMethod().getParameterTypes()[i], args.get(i-1)); + } catch (IllegalArgumentException e) + { + commandAnswer = "Failed to parse arguments, are they correct ? Call `/help " + fullCommand + "` for help."; + break; + } } + else + commandAnswer = "Invalid arguments. Call `/help " + fullCommand + "` for help."; } - else - commandAnswer = "Unknown command"; - } - catch (InvocationTargetException | IllegalAccessException e) - { - logger.severe("Couldn't get help: " + e.getMessage()); } + else + commandAnswer = "Unknown command. Call `/list 1` to see all available commands."; // Run command if(commandAnswer == null) diff --git a/src/main/java/net/pingex/discordbot/InternalCommandsModule.java b/src/main/java/net/pingex/discordbot/InternalCommandsModule.java index c743d34..6bbad9b 100644 --- a/src/main/java/net/pingex/discordbot/InternalCommandsModule.java +++ b/src/main/java/net/pingex/discordbot/InternalCommandsModule.java @@ -26,21 +26,40 @@ class InternalCommandsModule extends AbstractModule * @param command Full command, ie `module:command` * @return The man of this command */ - @Command(shorthand = "help") + @Command(shorthand = "help", description = "Gives the usage of a command.") public String help(MessageReceivedEvent event, String command) { - if(!command.contains(":") && CommandDispatcher.getInstance().getShortList().containsKey(command)) + // Shorthand and lower-case matcher + if(!command.contains(":") && CommandDispatcher.getInstance().getShortList().containsKey(command.toLowerCase())) command = CommandDispatcher.getInstance().getShortList().get(command.toLowerCase()); else command = command.toLowerCase(); + if(!CommandDispatcher.getInstance().getCommandList().containsKey(command)) return "Command not found."; - if(!CommandDispatcher.getInstance().getCommandList().containsKey(command)) return "No help available for this command."; InvokableMethod matchingMethod = CommandDispatcher.getInstance().getCommandList().get(command); + Map shorthands = CommandDispatcher.getInstance().getShortList(); + + StringBuffer toReturn = new StringBuffer(); + toReturn.append(Configuration.getValue("general", "commandPrefix")).append(command).append(" - "); + toReturn.append(matchingMethod.getMethod().getAnnotation(Command.class).description()); + + // Shorthand + if(shorthands.containsValue(command)) + { + toReturn.append(" (shorthand: `"); + for(Map.Entry i : shorthands.entrySet()) + if(i.getValue().equals(command)) + { + toReturn.append(i.getKey()); + break; + } + toReturn.append("`)"); + } - StringBuffer toReturn = new StringBuffer("Usage: "); - toReturn.append((Configuration.exists("general", "commandPrefix") ? Configuration.getValue("general", "commandPrefix") : "!") + command + " "); - for(int i=1; i i : matchingMethod.getMethod().getParameterTypes()) + if(i != MessageReceivedEvent.class) + toReturn.append(" <").append(i.getName()).append(">"); return toReturn.toString(); }