Skip to content
This repository has been archived by the owner on Feb 22, 2021. It is now read-only.

Using the Command Framework

Tobias Burdow [Kaleidox] edited this page May 24, 2019 · 2 revisions

The following is an example on how to use the included command framework:

// Create an instance of the framework using your Javacord DiscordAPI object
CommandHandler framework = new CommandHandler(discordApi);

// Define the prefixes for the commands
framework.prefixes = new String[]{"dango!", "d!"};

// Register the predefined help-command, if needed
// The parameter defines a supplier for the EmbedBuilder base. If null, defaults to DefaultEmbedFactory.INSTANCE
framework.useDefaultHelp(null); 

// Register the command classes you want to use
// registering a class with static command methods
framework.registerCommands(StaticCommands.class); 
// registering an object with non-static commands
framework.registerCommands(new Commands()); 
// registering just one method, must be static
framework.registerCommands(StaticCommands.class.getMethod("staticMethod" /* define parameters for Class#getMethod */ )); 

Responding on Commands can be easily done by returning whatever you want to respond with. Take a look at the Command annotation documentation for information about what a command method can return, and what parameters are allowed.

If a command method throws any exception, this exception is caught and rethrown as a RuntimeException. The command that caused the exception will also get a reaction that can be clicked to gather information about the exception thrown. Exmple code:

@Command
public void exception() throws IOException {
    throw new IOException("Could not read file!");
}

Example

A @Command annotation does not need any parameters. If there are no aliases defined, the method name is being used as the only alias. The following example will result in a command that can be called in our example via dango!about, which will send an Embed with only the Command author set as the Embed author:

// Define a CommandGroup for the commands in this class. 
// This will cause the default help command to categorize commands by groups with the same names.
// If no name is defined in the CommandGroup annotation, the class name is used instead.
@CommandGroup(name = "Basic Commands", description = "Basic bot usage commands")
public class Commands {
    @Command
    public EmbedBuilder about(User author) {
        return new EmbedBuilder().setAuthor(author);
    }
}
Clone this wiki locally