jda-kotlin-boilerplate/src/main/kotlin/com/github/mrtuxa/commands/ModalExample.kt

42 lines
1.7 KiB
Kotlin

package com.github.mrtuxa.commands
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent
import net.dv8tion.jda.api.hooks.ListenerAdapter
import net.dv8tion.jda.api.interactions.components.ActionRow
import net.dv8tion.jda.api.interactions.components.Modal
import net.dv8tion.jda.api.interactions.components.text.TextInput
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle
class ModalExample : ListenerAdapter() {
override fun onSlashCommandInteraction(event: SlashCommandInteractionEvent) {
if (event.name == "modmail") {
val subject = TextInput.create("subject", "Subject", TextInputStyle.SHORT)
.setPlaceholder("Subject of this ticket")
.setMinLength(10)
.setMaxLength(100) // or setRequiredRange(10, 100)
.build()
val body = TextInput.create("body", "Body", TextInputStyle.PARAGRAPH)
.setPlaceholder("Your concerns go here")
.setMinLength(30)
.setMaxLength(1000)
.build()
val modal = Modal.create("modmail", "Modmail")
.addActionRows(ActionRow.of(subject), ActionRow.of(body))
.build()
event.replyModal(modal).queue()
}
}
override fun onModalInteraction(event: ModalInteractionEvent) {
if (event.modalId == "modmail") {
val subject = event.getValue("subject") ?: return
val body = event.getValue("body") ?: return
event.reply("Thanks for your request!\nValues:\nSubject: $subject\nBody: $body").setEphemeral(true).queue()
}
}
}