Interface ButtonHandler


public interface ButtonHandler
An interface that contains the method that should be executed when a user interacts with a Button.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    handleButton(net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent event, net.dv8tion.jda.api.interactions.components.buttons.Button button)
    Method that must be overridden for all commands that handle button interactions.
  • Method Details

    • handleButton

      void handleButton(@Nonnull net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent event, @Nonnull net.dv8tion.jda.api.interactions.components.buttons.Button button)
      Method that must be overridden for all commands that handle button interactions. All buttons that match the identifier will execute this class's implementation of the handleButton method. This is best used with the ComponentIdBuilder.
      
       public class TestCommand extends SlashCommand implements ButtonHandler {
      
           public TestCommand() {
               setCommandData(Commands.slash("test", "test description"));
           }
      
          @Override
          public void execute(SlashCommandInteractionEvent event) {
       		event.reply("test")
       				.addActionRow(
       						Button.secondary(ComponentIdBuilder.build("test-button", 1), "Click me!"),
       						Button.secondary(ComponentIdBuilder.build("test-button", 2), "NO! Click me!")
       				).queue();
          }
      
           @Override
           public void handleButton(ButtonInteractionEvent event, Button button) {
       		String[] id = ComponentIdBuilder.split(button.getId());
       		String content = "";
       		switch (id[1]) {
       			case "1": content = "Thanks for not clicking the other button! :)"; break;
       			case "2": content = "Phew, thanks for clicking me..."; break;
               }
       		event.reply(content).queue();
          }
       }

      In order for this to work, you manually have to configure the corresponding button mappings like that:
      
       dih4JDA.addButtonMappings(IdMapping.of(new TestCommand(), "test-button"));
       
      Parameters:
      event - the ButtonInteractionEvent.
      button - the Button that the user interacted with.
      Since:
      v1.4
      See Also: