pingex
/
DiscordBot
Archived
1
0
Fork 0

Rewritten /help command.

master
Pingex aka Raphaël 9 years ago
parent 2856438460
commit 2d6e578feb

@ -95,48 +95,41 @@ class CommandDispatcher
Object[] parsedArray = null; Object[] parsedArray = null;
// Conditions // 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); InvokableMethod foundMethod = commandList.get(fullCommand);
parsedArray = new Object[foundMethod.getMethod().getParameterCount()]; parsedArray = new Object[foundMethod.getMethod().getParameterCount()];
parsedArray[0] = event; parsedArray[0] = event;
// To be redone, maybe ? // To be redone, maybe ?
Boolean canOverrideRun = PermissionsModule.getInstance().canRun(event.getMessage().getGuild(), event.getMessage().getAuthor(), fullCommand); Boolean canOverrideRun = PermissionsModule.getInstance().canRun(event.getMessage().getGuild(), event.getMessage().getAuthor(), fullCommand);
if(!foundMethod.getMethod().getAnnotation(Command.class).permission().eval(event)) if(!foundMethod.getMethod().getAnnotation(Command.class).permission().eval(event))
commandAnswer = "Permission denied."; commandAnswer = "Permission denied.";
if(canOverrideRun != null) if(canOverrideRun != null)
commandAnswer = canOverrideRun ? null : "Permission denied."; 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
for(int i=1; i < foundMethod.getMethod().getParameterCount(); i++) {
try parsedArray[i] = parse(foundMethod.getMethod().getParameterTypes()[i], args.get(i-1));
{ } catch (IllegalArgumentException e)
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;
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);
} }
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 // Run command
if(commandAnswer == null) if(commandAnswer == null)

@ -26,21 +26,40 @@ class InternalCommandsModule extends AbstractModule
* @param command Full command, ie `module:command` * @param command Full command, ie `module:command`
* @return The man of this 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) 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()); command = CommandDispatcher.getInstance().getShortList().get(command.toLowerCase());
else else
command = command.toLowerCase(); 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); InvokableMethod matchingMethod = CommandDispatcher.getInstance().getCommandList().get(command);
Map<String, String> 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<String, String> i : shorthands.entrySet())
if(i.getValue().equals(command))
{
toReturn.append(i.getKey());
break;
}
toReturn.append("`)");
}
StringBuffer toReturn = new StringBuffer("Usage: "); toReturn.append("\nUsage: ").append(Configuration.getValue("general", "commandPrefix")).append(command);
toReturn.append((Configuration.exists("general", "commandPrefix") ? Configuration.getValue("general", "commandPrefix") : "!") + command + " "); for(Class<?> i : matchingMethod.getMethod().getParameterTypes())
for(int i=1; i<matchingMethod.getMethod().getParameterCount(); i++) if(i != MessageReceivedEvent.class)
toReturn.append(matchingMethod.getMethod().getParameterTypes()[i].getName() + " "); toReturn.append(" <").append(i.getName()).append(">");
return toReturn.toString(); return toReturn.toString();
} }