Interface ContextMenuHandler


public interface ContextMenuHandler
Handler for context menu commands.

Context menus appear when right-clicking on users or messages.

Preferred approach: Use the @ContextMenu annotation directly on methods in your @Plugin class:

@Plugin(name = "MyPlugin", version = "1.0.0", author = "Author")
public class MyPlugin {

    @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();
    }

    @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();
    }
}

Alternative: Implement this interface and register via InteractionManager for more control (e.g., guild-specific registration):

Example (User Context Menu):


public class UserInfoContextMenu implements ContextMenuHandler {
    @Override
    public CommandData getCommandData() {
        return Commands.user("Get User Info");
    }

    @Override
    public void handleUserContext(UserContextInteractionEvent event) {
        User target = event.getTarget();
        event.reply("User: " + target.getAsTag() + "\nID: " + target.getId()).queue();
    }
}

Example (Message Context Menu):


public class BookmarkContextMenu implements ContextMenuHandler {
    @Override
    public CommandData getCommandData() {
        return Commands.message("Bookmark Message");
    }

    @Override
    public void handleMessageContext(MessageContextInteractionEvent event) {
        Message message = event.getTarget();
        // Save bookmark...
        event.reply("Message bookmarked!").setEphemeral(true).queue();
    }
}

Register in your @Plugin class:

@OnEnable
public void onEnable(PluginContext context) {
    context.getInteractionManager().registerContextMenu("my-plugin", new UserInfoContextMenu());
}

  • Method Summary

    Modifier and Type
    Method
    Description
    net.dv8tion.jda.api.interactions.commands.build.CommandData
    Get the context menu command data.
    default long[]
    Get guild IDs for guild-specific registration.
    default void
    handleMessageContext(net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent event)
    Handle message context menu interaction.
    default void
    handleUserContext(net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent event)
    Handle user context menu interaction.
    default boolean
    Whether this command should be registered globally.
  • Method Details

    • getCommandData

      net.dv8tion.jda.api.interactions.commands.build.CommandData getCommandData()
      Get the context menu command data.

      Use Commands.user(String) for user context menus, or Commands.message(String) for message context menus.

      Returns:
      the command data
    • handleUserContext

      default void handleUserContext(net.dv8tion.jda.api.events.interaction.command.UserContextInteractionEvent event)
      Handle user context menu interaction.

      Called when this command is used on a user (right-click → Apps → Your Command).

      Parameters:
      event - the user context interaction event
    • handleMessageContext

      default void handleMessageContext(net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionEvent event)
      Handle message context menu interaction.

      Called when this command is used on a message (right-click → Apps → Your Command).

      Parameters:
      event - the message context interaction event
    • isGlobal

      default boolean isGlobal()
      Whether this command should be registered globally.
      Returns:
      true for global registration
    • getGuildIds

      default long[] getGuildIds()
      Get guild IDs for guild-specific registration.
      Returns:
      array of guild IDs, or null for all guilds