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)
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 {};
}

@ -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();
}
});
}

@ -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<String, InvokableMethod> commands = CommandDispatcher.getInstance().getCommandList();
Map<String, String> shorthands = CommandDispatcher.getInstance().getShortList();
if(page == null) page = 1;
int pagesCount = (int) Math.ceil(commands.size()/10.0);
if(page > pagesCount || page <= 0)