pingex
/
DiscordBot
Archived
1
0
Fork 0

Optional command arguments. Also closes #12.

master
Pingex aka Raphaël 9 years ago
parent ee0d746a04
commit 7fb574a401

@ -14,11 +14,6 @@ import java.lang.annotation.Target;
@Target(ElementType.METHOD) @Target(ElementType.METHOD)
public @interface Command public @interface Command
{ {
/**
* Minimum number of arguments expected
*/
int minArgs() default 0;
/** /**
* Shortened command * Shortened command
*/ */
@ -33,4 +28,9 @@ public @interface Command
* Description of the command * Description of the command
*/ */
String description() default "No description provided for this 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 {};
} }

@ -1,5 +1,6 @@
package net.pingex.discordbot; package net.pingex.discordbot;
import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory; import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import sx.blah.discord.api.EventSubscriber; import sx.blah.discord.api.EventSubscriber;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent; import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
@ -112,24 +113,37 @@ class CommandDispatcher
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++) for(int i=1; i < foundMethod.getMethod().getParameterCount(); i++)
{
try 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) } 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; break;
} }
}
} }
else else
commandAnswer = "Invalid arguments. Call `/help " + fullCommand + "` for help."; commandAnswer = "Invalid arguments. Call `" + Configuration.getValue("general", "commandPrefix") + "help " + fullCommand + "` for help.";
} }
} }
else 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 // Run command
if(commandAnswer == null) if(commandAnswer == null)
@ -151,10 +165,6 @@ class CommandDispatcher
} catch (MissingPermissionsException | HTTP429Exception | DiscordException e) } catch (MissingPermissionsException | HTTP429Exception | DiscordException e)
{ {
logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage()); logger.warning("Couldn't reply to command (" + e.getClass().getName() + "): " + e.getMessage());
} catch (Exception e)
{
logger.severe("Error in threaded command");
e.printStackTrace();
} }
}); });
} }

@ -69,11 +69,12 @@ class InternalCommandsModule extends AbstractModule
* @param page Page number * @param page Page number
* @return String representation of this command * @return String representation of this command
*/ */
@Command(shorthand = "list", description = "List all commands available to you.") @Command(shorthand = "list", description = "List all commands available to you.", required = {false})
public String list(MessageReceivedEvent event, int page) public String list(MessageReceivedEvent event, Integer page)
{ {
Map<String, InvokableMethod> commands = CommandDispatcher.getInstance().getCommandList(); Map<String, InvokableMethod> commands = CommandDispatcher.getInstance().getCommandList();
Map<String, String> shorthands = CommandDispatcher.getInstance().getShortList(); Map<String, String> shorthands = CommandDispatcher.getInstance().getShortList();
if(page == null) page = 1;
int pagesCount = (int) Math.ceil(commands.size()/10.0); int pagesCount = (int) Math.ceil(commands.size()/10.0);
if(page > pagesCount || page <= 0) if(page > pagesCount || page <= 0)