From 7fb574a401667dda19b0e26c1d1358b97988408b Mon Sep 17 00:00:00 2001 From: Pingex Date: Sun, 8 May 2016 01:41:40 +0200 Subject: [PATCH] Optional command arguments. Also closes #12. --- .../java/net/pingex/discordbot/Command.java | 10 +++---- .../pingex/discordbot/CommandDispatcher.java | 28 +++++++++++++------ .../discordbot/InternalCommandsModule.java | 5 ++-- 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/pingex/discordbot/Command.java b/src/main/java/net/pingex/discordbot/Command.java index e40c216..329e465 100644 --- a/src/main/java/net/pingex/discordbot/Command.java +++ b/src/main/java/net/pingex/discordbot/Command.java @@ -14,11 +14,6 @@ import java.lang.annotation.Target; @Target(ElementType.METHOD) public @interface Command { - /** - * Minimum number of arguments expected - */ - int minArgs() default 0; - /** * Shortened command */ @@ -33,4 +28,9 @@ public @interface Command * Description of the command */ String description() default "No description provided for this command."; + + /** + * Arguments that are required, or not. `true` for a required field, `false` for a not required one. + */ + boolean[] required() default {}; } diff --git a/src/main/java/net/pingex/discordbot/CommandDispatcher.java b/src/main/java/net/pingex/discordbot/CommandDispatcher.java index abcd4dd..9a3664c 100644 --- a/src/main/java/net/pingex/discordbot/CommandDispatcher.java +++ b/src/main/java/net/pingex/discordbot/CommandDispatcher.java @@ -1,5 +1,6 @@ package net.pingex.discordbot; +import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.concurrent.BasicThreadFactory; import sx.blah.discord.api.EventSubscriber; import sx.blah.discord.handle.impl.events.MessageReceivedEvent; @@ -112,24 +113,37 @@ class CommandDispatcher 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)); + parsedArray[i] = parse(foundMethod.getMethod().getParameterTypes()[i], args.size() > i-1 ? args.get(i-1) : null); } catch (IllegalArgumentException e) { - commandAnswer = "Failed to parse arguments, are they correct ? Call `/help " + fullCommand + "` for help."; + commandAnswer = "Invalid arguments. Call `" + Configuration.getValue("general", "commandPrefix") + "help " + fullCommand + "` for help."; + } + + // Try to know if the field is required (or not) from the Command annotation + boolean isRequired = true; + try { isRequired = foundMethod.getMethod().getAnnotation(Command.class).required()[i-1]; } catch (ArrayIndexOutOfBoundsException e) {} + + // if(ArgumentIsNull && (IsRequired || ArgumentIsRealPrimitiveOrObject)) + if(parsedArray[i] == null && (isRequired || + !(ClassUtils.isPrimitiveWrapper(foundMethod.getMethod().getParameterTypes()[i]) || foundMethod.getMethod().getParameterTypes()[i] == String.class))) + { + commandAnswer = "Invalid arguments. Call `" + Configuration.getValue("general", "commandPrefix") + "help " + fullCommand + "` for help."; break; } + } } else - commandAnswer = "Invalid arguments. Call `/help " + fullCommand + "` for help."; + commandAnswer = "Invalid arguments. Call `" + Configuration.getValue("general", "commandPrefix") + "help " + fullCommand + "` for help."; } } else - commandAnswer = "Unknown command. Call `/list 1` to see all available commands."; + commandAnswer = "Unknown command. Call `" + Configuration.getValue("general", "commandPrefix") + "list` to see all available commands."; // Run command if(commandAnswer == null) @@ -151,10 +165,6 @@ class CommandDispatcher } catch (MissingPermissionsException | HTTP429Exception | DiscordException e) { logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage()); - } catch (Exception e) - { - logger.severe("Error in threaded command"); - e.printStackTrace(); } }); } diff --git a/src/main/java/net/pingex/discordbot/InternalCommandsModule.java b/src/main/java/net/pingex/discordbot/InternalCommandsModule.java index 14c5fcf..d152487 100644 --- a/src/main/java/net/pingex/discordbot/InternalCommandsModule.java +++ b/src/main/java/net/pingex/discordbot/InternalCommandsModule.java @@ -69,11 +69,12 @@ class InternalCommandsModule extends AbstractModule * @param page Page number * @return String representation of this command */ - @Command(shorthand = "list", description = "List all commands available to you.") - public String list(MessageReceivedEvent event, int page) + @Command(shorthand = "list", description = "List all commands available to you.", required = {false}) + public String list(MessageReceivedEvent event, Integer page) { Map commands = CommandDispatcher.getInstance().getCommandList(); Map shorthands = CommandDispatcher.getInstance().getShortList(); + if(page == null) page = 1; int pagesCount = (int) Math.ceil(commands.size()/10.0); if(page > pagesCount || page <= 0)