pingex
/
DiscordBot
Archived
1
0
Fork 0

Rewritten /help command.

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

@ -95,8 +95,6 @@ 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);
@ -122,21 +120,16 @@ class CommandDispatcher
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 ? " + commandList.get("internal:help").invoke(event, fullCommand); commandAnswer = "Failed to parse arguments, are they correct ? Call `/help " + fullCommand + "` for help.";
break; break;
} }
} }
else else
commandAnswer = "Invalid arguments. " + commandList.get("internal:help").invoke(event, fullCommand); commandAnswer = "Invalid arguments. Call `/help " + fullCommand + "` for help.";
} }
} }
else else
commandAnswer = "Unknown command"; commandAnswer = "Unknown command. Call `/list 1` to see all available commands.";
}
catch (InvocationTargetException | IllegalAccessException e)
{
logger.severe("Couldn't get help: " + e.getMessage());
}
// 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();
} }