Annotation Interface Command


@Target(METHOD) @Retention(RUNTIME) public @interface Command

Methods annotated with Command will be registered as a slash command at startup.

Therefore, the method must be declared inside a class that is annotated with Interaction. Both slash commands and context commands are registered via this annotation.

1. Slash Commands

The method signature has to meet the following conditions:

  • First parameter must be of type CommandEvent
  • Remaining parameter types must be registered at the TypeAdapters. Please note that Optional are handled special, see next headline.

Optional as parameter type

Beside defining parameters with Param.optional() set to true, it's also possible to wrap it in an Optional with the appropriated type set as the generic type parameter. (Example: @Param(optional = true) String one -> Optional<String> one.

JDA-Commands will register this parameter as optional to discord. If the option isn't provided by the user, Optional.empty() is passed.

Examples:

@Command("greet")
public void onCommand(CommandEvent event) {
    event.reply("Hello World!");
}

@Command(value="moderation ban", desc="Bans a member", enabledFor=Permission.BAN_MEMBERS)
public void onCommand(CommandEvent event, @Param("The member to ban") Member target, @Param(optional = true, fallback = "no reason given") String reason) { ... }

@Command(value = "favourite fruit")
public void onCommand(CommandEvent event, @Choices({"Apple", "Banana", "Orange"}) String fruit) {
    event.reply("You've chosen: %s", fruit);
}

Context Commands

The method signature has to meet the following conditions:

Examples:

@Command(value = "message context command", type = Type.MESSAGE)
public void onCommand(CommandEvent event, Message target) { ... }

@Command(value = "user context command", type = Type.USER)
public void onCommand(CommandEvent event, User target) { ... }

@Command(value = "member context command", type = Type.USER)
public void onCommand(CommandEvent event, Member target) { ... }

Using Member will enforce InteractionContextType.GUILD on the command!

See Also:
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Returns the description of the command.
    Gets the type of this command.
    Returns the name of the command.
  • Element Details

    • value

      String value
      Returns the name of the command.
      Returns:
      the name of the command
      Default:
      ""
    • desc

      String desc
      Returns the description of the command.
      Returns:
      the description of the command
      Default:
      "N/A"
    • type

      Gets the type of this command.
      Returns:
      the type of the command
      Default:
      SLASH