pingex
/
DiscordBot
Archived
1
0
Fork 0

New `InternalCommandsModule`

Moved help command to `InternalCommandsModule`
`CommandDispatcher` as a singleton
master
Pingex aka Raphaël 9 years ago
parent 0df7e7b4f0
commit 039e62b311

@ -24,6 +24,11 @@ class CommandDispatcher
{ {
private Logger logger; private Logger logger;
/**
* Singleton instance
*/
private static final CommandDispatcher INSTANCE = new CommandDispatcher();
/** /**
* Contains all available commands, built using `rebuildCommandList()` * Contains all available commands, built using `rebuildCommandList()`
*/ */
@ -47,7 +52,7 @@ class CommandDispatcher
/** /**
* Basic Constructor, automatically rebuilds command list from the modules registry * Basic Constructor, automatically rebuilds command list from the modules registry
*/ */
public CommandDispatcher() private CommandDispatcher()
{ {
logger = Logger.getLogger(this.getClass().getName()); logger = Logger.getLogger(this.getClass().getName());
rebuildCommandList(); rebuildCommandList();
@ -62,6 +67,15 @@ class CommandDispatcher
} }
} }
/**
* Gets the unique instance of `CommandDispatcher`
* @return The current instance of `CommandDispatcher`
*/
public static CommandDispatcher getInstance()
{
return INSTANCE;
}
/** /**
* Fired when receiving a message * Fired when receiving a message
* @param event Event from Discord API * @param event Event from Discord API
@ -87,31 +101,38 @@ class CommandDispatcher
Object[] parsedArray = null; Object[] parsedArray = null;
// Conditions // Conditions
if(commandList.containsKey(fullCommand)) try
{ {
logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand); if(commandList.containsKey(fullCommand))
{
logger.info("Command invoked (" + event.getMessage().getAuthor().getName() + "#" + event.getMessage().getAuthor().getDiscriminator() + "): " + fullCommand);
InvokableMethod foundMethod = commandList.get(fullCommand); InvokableMethod foundMethod = commandList.get(fullCommand);
parsedArray = new Object[foundMethod.getMethod().getParameterCount()]; parsedArray = new Object[foundMethod.getMethod().getParameterCount()];
parsedArray[0] = event; parsedArray[0] = event;
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.get(i-1));
} catch (IllegalArgumentException e) } catch (IllegalArgumentException e)
{ {
commandAnswer = "Failed to parse arguments, are they correct ? " + getHelp(fullCommand); commandAnswer = "Failed to parse arguments, are they correct ? " + commandList.get("internal:help").invoke(event, fullCommand);
break; break;
} }
}
else
commandAnswer = "Invalid arguments. " + commandList.get("internal:help").invoke(event, fullCommand);
} }
else else
commandAnswer = "Invalid arguments. " + getHelp(fullCommand); commandAnswer = "Unknown command";
}
catch (InvocationTargetException | IllegalAccessException e)
{
logger.severe("Couldn't get help: " + e.getMessage());
} }
else
commandAnswer = "Unknown command";
// Run command // Run command
if(commandAnswer == null) if(commandAnswer == null)
@ -149,7 +170,7 @@ class CommandDispatcher
/** /**
* Rebuilds command list using the module registry. * Rebuilds command list using the module registry.
* - It looks for methods with @Command annotation inside class with @Controllable annotation * - It looks for methods with @Command annotation inside class with @Controllable annotation
* - Check his prototype (should be `method(String[]):String`) * - Check his prototype (should be `method(IDiscordClient, ...):String`)
* - Add usable methods in his local registry, using InvokableMethod * - Add usable methods in his local registry, using InvokableMethod
* @see InvokableMethod * @see InvokableMethod
* @see ModulesRegistry * @see ModulesRegistry
@ -213,20 +234,20 @@ class CommandDispatcher
} }
/** /**
* Scans and print the usage of a command * Gets the command list (cloned to avoid modifications)
* @param command Full command `module:command` * @return The current cloned command list
* @return Human-readable string
*/ */
private String getHelp(String command) public HashMap<String, InvokableMethod> getCommandList()
{ {
if(!commandList.containsKey(command)) return "No help available for this command."; return (HashMap<String, InvokableMethod>) commandList.clone();
InvokableMethod matchingMethod = commandList.get(command); }
StringBuffer toReturn = new StringBuffer("Usage: ");
toReturn.append(commandPrefix + command + " ");
for(int i=1; i<matchingMethod.getMethod().getParameterCount(); i++)
toReturn.append(matchingMethod.getMethod().getParameterTypes()[i].getName() + " ");
return toReturn.toString(); /**
* Gets the shorthands list (cloned to avoid edits)
* @return The current cloned short list
*/
public HashMap<String, String> getShortList()
{
return (HashMap<String, String>) shortList.clone();
} }
} }

@ -36,13 +36,16 @@ class DiscordBot
LOGGER.info("Logged in as \"" + event.getClient().getOurUser().getName() + "#" + event.getClient().getOurUser().getDiscriminator() + "\" (#" + event.getClient().getOurUser().getID() + ")")); LOGGER.info("Logged in as \"" + event.getClient().getOurUser().getName() + "#" + event.getClient().getOurUser().getDiscriminator() + "\" (#" + event.getClient().getOurUser().getID() + ")"));
enableModules(); enableModules();
this.client.getDispatcher().registerListener(new CommandDispatcher()); this.client.getDispatcher().registerListener(CommandDispatcher.getInstance());
} }
private void enableModules() private void enableModules()
{ {
LOGGER.info("Enabling modules"); LOGGER.info("Enabling modules");
// Hardcoded internal modules
new InternalCommandsModule(client);
if(!Configuration.exists("plugins", "enable") || Configuration.getValue("plugins", "enable").isEmpty()) if(!Configuration.exists("plugins", "enable") || Configuration.getValue("plugins", "enable").isEmpty())
{ {
LOGGER.warning("Key \"plugins/enable\" doesn't exist in Configuration. No module will be enabled."); LOGGER.warning("Key \"plugins/enable\" doesn't exist in Configuration. No module will be enabled.");

@ -0,0 +1,42 @@
package net.pingex.discordbot;
import sx.blah.discord.api.IDiscordClient;
import sx.blah.discord.handle.impl.events.MessageReceivedEvent;
import java.util.HashMap;
/**
* Internal commands of the bot, such as `help`, and so on
* @version 0.1-dev
* @author Raphael "Pingex" NAAS
*/
@Controllable(name="internal")
public class InternalCommandsModule extends AbstractModule
{
public InternalCommandsModule(IDiscordClient client)
{
super(client);
}
/**
* Scans and print the usage of a command
* @param command Full command, ie `module:command`
* @return The man of this command
*/
@Command(shorthand = "help")
public String help(MessageReceivedEvent event, String command)
{
if(!command.contains(":") && CommandDispatcher.getInstance().getShortList().containsKey(command))
command = CommandDispatcher.getInstance().getShortList().get(command);
if(!CommandDispatcher.getInstance().getCommandList().containsKey(command)) return "No help available for this command.";
InvokableMethod matchingMethod = CommandDispatcher.getInstance().getCommandList().get(command);
StringBuffer toReturn = new StringBuffer("Usage: ");
toReturn.append((Configuration.exists("general", "commandPrefix") ? Configuration.getValue("general", "commandPrefix") : "!") + command + " ");
for(int i=1; i<matchingMethod.getMethod().getParameterCount(); i++)
toReturn.append(matchingMethod.getMethod().getParameterTypes()[i].getName() + " ");
return toReturn.toString();
}
}