Interface ModalHandler


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

    Modifier and Type
    Method
    Description
    void
    handleModal(net.dv8tion.jda.api.events.interaction.ModalInteractionEvent event, List<net.dv8tion.jda.api.interactions.modals.ModalMapping> values)
    Method that must be overridden for all Commands that handle Modal Interactions.
  • Method Details

    • handleModal

      void handleModal(@Nonnull net.dv8tion.jda.api.events.interaction.ModalInteractionEvent event, @Nonnull List<net.dv8tion.jda.api.interactions.modals.ModalMapping> values)
      Method that must be overridden for all Commands that handle Modal Interactions. All modals that match the identifier will execute this class's implementation of the handleModal method. This is best used with the ComponentIdBuilder.
      
       public class TestCommand extends SlashCommand implements ModalHandler {
      
           public TestCommand() {
               setCommandData(Commands.slash("test", "test description"));
           }
      
           @Override
           public void execute(SlashCommandInteractionEvent event) {
       		Role applied = [...]
       		TextInput email = TextInput.create("email", "Email", TextInputStyle.SHORT)
       				.setPlaceholder("Enter your E-mail")
       				.setMinLength(10)
       				.setMaxLength(100)
       				.build();
      
       		TextInput body = TextInput.create("body", "Body", TextInputStyle.PARAGRAPH)
       				.setPlaceholder("Your application goes here")
       				.setMinLength(30)
       				.setMaxLength(1000)
       				.build();
      
       		Modal modal = Modal.create(ComponentIdBuilder.build("test-modal", applied.getIdLong()), "Apply for " + applied.getName())
       				.addActionRows(ActionRow.of(email), ActionRow.of(body))
       				.build();
       		event.replyModal(modal).queue();
          }
      
          @Override
          public void handleModal(ModalInteractionEvent event, List<ModalMapping> values) {
            Role role = event.getGuild().getRoleById(ComponentIdBuilder.split(event.getModalId())[1]);
       		String email = event.getValue("email").getAsString();
       		String body = event.getValue("body").getAsString();
      
       		createApplication(role, email, body);
      
       		event.reply("Thanks for your application!").queue();
          }
       }

      In order for this to work, you manually have to configure the corresponding modal mappings like that:
      
       dih4JDA.addModalMappings(IdMapping.of(new TestCommand(), "test-modal"));
       
      Parameters:
      event - the provided ModalInteractionEvent.
      values - a List of the ModalMapping values.
      Since:
      v1.4
      See Also: