Interface ButtonHandler


public interface ButtonHandler
Handler for button interactions.

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

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

    @code @ButtonHandler("myplugin:")
    public void handleButton(ButtonInteractionEvent event) {
        String buttonId = event.getComponentId();
        if (buttonId.equals("myplugin:confirm")) {
            event.reply("Confirmed!").setEphemeral(true).queue();
        }
    }
}

Alternative: Implement this interface and register via InteractionManager:


public class MyButtonHandler implements ButtonHandler {
    @Override
    public String getButtonIdPrefix() {
        return "myplugin:";
    }

    @Override
    public void handle(ButtonInteractionEvent event) {
        String buttonId = event.getComponentId();
        if (buttonId.equals("myplugin:confirm")) {
            event.reply("Confirmed!").setEphemeral(true).queue();
        }
    }
}

  • Method Summary

    Modifier and Type
    Method
    Description
    Get the button ID prefix this handler responds to.
    void
    handle(net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent event)
    Handle the button interaction.
  • Method Details

    • getButtonIdPrefix

      String getButtonIdPrefix()
      Get the button ID prefix this handler responds to.

      The handler will receive all button interactions where the component ID starts with this prefix.

      Use a unique prefix like "pluginname:" to avoid conflicts.

      Returns:
      the button ID prefix
    • handle

      void handle(net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent event)
      Handle the button interaction.

      You must respond to the interaction within 3 seconds using one of:

      • event.reply(...)
      • event.deferReply()
      • event.editMessage(...)
      • event.deferEdit()
      Parameters:
      event - the button interaction event