Skip to content
This repository has been archived by the owner on Oct 30, 2022. It is now read-only.

Creating a help command

Bastian Oppermann edited this page Nov 16, 2017 · 4 revisions

One great advantage of sdcf4j are custom help commands.

To create a help command you will need the following methods:

  • CommandHandler#getCommands(): Gets a list with all command in the order they were registered.
  • SimpleCommand#getCommandAnnotation(): Gets the @Command annotation of the command.
  • CommandHandler#getDefaultPrefix(): Gets the default command prefix. Can not be null, but can be an empty String.

The following parameters of the @Command annotation are important for help commands:

  • String description: The description of the command (default: "none").
  • String usage: The usage of the command (e.g. "!info [author|time]"; default: "").
  • boolean showInHelpPage: Whether the command should be shown in the help page or not (default: true).

An example implementation of a help command can look like this:

package <your.package>;

import de.btobastian.sdcf4j.Command;
import de.btobastian.sdcf4j.CommandExecutor;
import de.btobastian.sdcf4j.CommandHandler;

public class HelpCommand implements CommandExecutor {

    private final CommandHandler commandHandler;

    public HelpCommand(CommandHandler commandHandler) {
        this.commandHandler = commandHandler;
    }

    @Command(aliases = {"!help", "!commands"}, description = "Shows this page")
    public String onHelpCommand() {
        StringBuilder builder = new StringBuilder();
        builder.append("```xml"); // a xml code block looks fancy
        for (CommandHandler.SimpleCommand simpleCommand : commandHandler.getCommands()) {
            if (!simpleCommand.getCommandAnnotation().showInHelpPage()) {
                continue; // skip command
            }
            builder.append("\n");
            if (!simpleCommand.getCommandAnnotation().requiresMention()) {
                // the default prefix only works if the command does not require a mention
                builder.append(commandHandler.getDefaultPrefix());
            }
            String usage = simpleCommand.getCommandAnnotation().usage();
            if (usage.isEmpty()) { // no usage provided, using the first alias
                usage = simpleCommand.getCommandAnnotation().aliases()[0];
            }
            builder.append(usage);
            String description = simpleCommand.getCommandAnnotation().description();
            if (!description.equals("none")) {
                builder.append(" | ").append(description);
            }
        }
        builder.append("\n```"); // end of xml code block
        return builder.toString();
    }

}

In Discord it would look like this: example

You can use the same system to create more fancy help pages with embeds etc. of course.