Skip to content

Create a Slash Command

Robin Mercier edited this page Jun 15, 2021 · 3 revisions

1. Create a new class

public final class ExampleCommand {}

2. Add the @Slash.Tag annotation

The value of the @Slash.Tag will be the tag (or key) you will use to retrieve the command from the SlashClient. The tag value must be unique.

Note: When retrieving a SlashCommand, we use a tag rather than the command's name as a global and a guild command can have the same name.

@Slash.Tag("example")
public final class ExampleCommand {}

3. Add the @Slash.Command annotation

When adding the @Slash.Command annotation, you need to at least define the name and description of the command.

@Slash.Tag("example")
@Slash.Command(
    name = "example",
    description = "For demonstration purposes only"
)
public final class ExampleCommand {}

The above example would equate in Discord to:

/example

4. Personalise your command

The @Slash.Command has 4 non-required parameters that you can set to your liking.

  • Option[] options - the option list of the command
  • Subcommand[] subcommands - the subcommand list of the command
  • SubcommandGroup[] subcommandGroups - the subcommand group list of the command
  • boolean enabled - whether the command is enabled by default when the bot is added to a guild

In the case of the 3 first parameters, only one of them can be set at a time (per command).
For instance, the following is invalid:

// INVALID
@Slash.Command(
    name = "example",
    description = "An invalid command",
    options = { ... },
    subcommands = { ... }
)

// VALID
@Slash.Command(
    name = "example",
    description = "A valid command with options",
    options = { ... }
)

// VALID
@Slash.Command(
    name = "example",
    description = "A valid command with subcommands",
    subcommands = { ... }
)
4.1. Subcommands groups

The subcommand groups of a command are defined as follows:

subcommandGroups = {
    @SubcommandGroup(
        name = "admin",
        description = "Edit the admin registry",
        subcommands = { ... }
    ),
    @SubcommandGroup(
        name = "member",
        description = "Edit the member registry",
        subcommands = { ... }
    )
}

Where the subcommands are non-required parameters.

4.2. Subcommands

The options of either a command or a subcommand group are defined as follows:

subcommands = {
    @Subcommand(
        name = "add",
        description = "Add a user",
        options = { ... }
    ),
    @Subcommand(
        name = "remove",
        description = "Remove a user",
        options = { ... }
    )
}

Where the options are non-required parameters.

4.3. Options

The options of either a command or a subcommand are defined as follows:

options = {
    @Option(
        name = "name",
        description = "The name of the user",
        type = OptionType.STRING
    ),
    @Option(
        name = "age",
        description = "The age of the user",
        type = OptionType.INTEGER
    )
}

Note: The different OptionType are:
STRING
INTEGER
BOOLEAN
USER
CHANNEL
ROLE
MENTIONABLE - either a Member or a Role

An @Option has 2 non-required parameters:

  • boolean required - whether the option is required or optional
  • Choice[] choices - the choices list of the option

The choices are defined as follows:

@Option(
    name = "gender",
    description = "The gender of the user",
    type = OptionType.STRING,
    choices = {
        @Choice(
            name = "Male",
            value = "MALE"
        ),
        @Choice(
            name = "Female",
            value = "FEMALE"
        )
    }
)

Note: The only @Option types that support choices are OptionType.STRING and OptionType.INTEGER.

Note: If the type of the @Option is OptionType.INTEGER, the value of the @Choice must only contain digits. Otherwise, in the case of the OptionType.STRING, the value must not have more than 100 characters.