Annotation Interface ContextMenu


@Retention(RUNTIME) @Target(METHOD) public @interface ContextMenu
Marks a method as a context menu handler.

Context menus appear when right-clicking on users or messages in Discord. The method will be called with either a UserContextInteractionEvent or MessageContextInteractionEvent.

User Context Menu Example:

@ContextMenu(name = "Get User Info", type = Command.Type.USER)
public void getUserInfo(UserContextInteractionEvent event) {
    User target = event.getTarget();
    event.reply("User: " + target.getAsTag() + "\nID: " + target.getId()).queue();
}

Message Context Menu Example:

@ContextMenu(name = "Bookmark Message", type = Command.Type.MESSAGE)
public void bookmarkMessage(MessageContextInteractionEvent event) {
    Message message = event.getTarget();
    // Save bookmark...
    event.reply("Message bookmarked!").setEphemeral(true).queue();
}

The core automatically:

  • Registers the command when plugin is enabled
  • Syncs to Discord (globally by default)
  • Unregisters and re-syncs when plugin is disabled
  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
    Returns the display name of the context menu.
    net.dv8tion.jda.api.interactions.commands.Command.Type
    Returns the type of context menu this command represents.
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    boolean
    Indicates whether this context menu should be registered globally or per-guild.
    long[]
    Returns an array of guild IDs where this context menu should be registered.
  • Element Details

    • name

      String name
      Returns the display name of the context menu. This name is shown to users in the Discord client when they right-click on a user or message.
      Returns:
      the name of the context menu (Max 32 characters)
    • type

      net.dv8tion.jda.api.interactions.commands.Command.Type type
      Returns the type of context menu this command represents.

      The type determines whether the context menu appears when right-clicking on a user or a message. For user context menus, the handler receives a UserContextInteractionEvent. For message context menus, the handler receives a MessageContextInteractionEvent.

      Returns:
      the context menu type (USER or MESSAGE)
    • global

      boolean global
      Indicates whether this context menu should be registered globally or per-guild.

      When true, the context menu will be available across all guilds the bot is in. When false, the context menu must be explicitly registered per guild using guildIds().

      Returns:
      true if the context menu should be registered globally, false otherwise; Default true
      Default:
      true
    • guildIds

      long[] guildIds
      Returns an array of guild IDs where this context menu should be registered.

      This method is used when global() is set to false to specify the exact guilds where the context menu should appear. If global() is true, this setting has no effect and the context menu will be registered globally across all guilds.

      Returns:
      an array of guild IDs, or an empty array if not specified or if global registration is enabled
      Default:
      {}