Skip to content

Create Slash Command callbacks

Robin Mercier edited this page Jun 15, 2021 · 1 revision

Creating a Slash Command callback is simply done by adding a @Slash.Handler annotation to a method.

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

    /* Methods */

    @Slash.Handler()
    public void example(SlashCommandEvent event) {}
}

There are still a few things to be aware of:

  • the method must only have one parameter, being of the SlashCommandEvent class type
  • the method must be public
  • there can only be one single callback per defined path

Subcommand groups and subcommands callbacks

The @Slash.Handler has a single non-required parameter value which corresponds to the command path of the callback.

Note: The path (value) of the @Slash.Handler must not contain the command name.

The callback paths are structured the following way:

  • "" - the root of a command
  • "group" - a subcommand group of a command
  • "group/subcommand" - a subcommand of a command

The following command paths would equate to the following callback paths:

registry               ->  @Slash.Handler("")
registry/admin         ->  @Slash.Handler("admin")
registry/admin/add     ->  @Slash.Handler("admin/add")
registry/admin/remove  ->  @Slash.Handler("admin/remove")

Special mappings

If no callback path matches exactly a command path, the command path will fallback by default to a parent path (if any).
In addition, it is possible to define a single callback to multiple subcommands (regardless of their subcommand groups) if they all share the same name. To do so, replace the subcommand group name by a * in the callback path.

Having these callback paths defined:

""
"admin"
"*/add"

The command paths would be mapped the following way:

registry/admin/add      ->  @Slash.Handler("*/add")
registry/admin/edit     ->  @Slash.Handler("admin")
registry/admin/remove   ->  @Slash.Handler("admin")
registry/member/add     ->  @Slash.Handler("*/add")
registry/member/edit    ->  @Slash.Handler("")
registry/member/remove  ->  @Slash.Handler("")