Skip to content

Commit

Permalink
refactor: extract command modal and text field ids to a constant
Browse files Browse the repository at this point in the history
  • Loading branch information
shaksternano committed Sep 30, 2024
1 parent e888eed commit db379cc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ suspend fun JDA.registerCommands() {
listener<CommandAutoCompleteInteractionEvent> {
handleCommandAutoComplete(it)
}
val commandModalInteractionName = "Run command"
val commandModalInteractionName = "Run Command"
listener<MessageContextInteractionEvent> {
if (it.name == commandModalInteractionName) {
createCommandModal(it)
Expand All @@ -56,7 +56,7 @@ suspend fun JDA.registerCommands() {
handleUserInteraction(it.convert())
}
listener<ModalInteractionEvent> {
if (it.modalId == "command") {
if (it.modalId == COMMAND_MODAL_ID) {
handleModalCommand(it)
} else {
logger.error("Unknown modal ID: ${it.modalId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,21 @@ import net.dv8tion.jda.api.events.interaction.command.MessageContextInteractionE
import net.dv8tion.jda.api.interactions.components.ActionRow
import net.dv8tion.jda.api.interactions.components.text.TextInputStyle

const val COMMAND_MODAL_ID = "command"
const val COMMAND_MODAL_TEXT_INPUT_ID = "command"

suspend fun createCommandModal(event: MessageContextInteractionEvent) {
val command = TextInput(
id = "command",
id = COMMAND_MODAL_TEXT_INPUT_ID,
label = "Command",
style = TextInputStyle.SHORT,
) {
placeholder = "Enter the command you want to execute on this message"
builder.minLength = 1
}
val modal = Modal(
id = "command",
title = "Run command",
id = COMMAND_MODAL_ID,
title = "Run Command",
) {
components += ActionRow.of(command)
}
Expand All @@ -51,7 +54,7 @@ suspend fun createCommandModal(event: MessageContextInteractionEvent) {
}

suspend fun handleModalCommand(event: ModalInteractionEvent) {
val content = event.getValue("command")
val content = event.getValue(COMMAND_MODAL_TEXT_INPUT_ID)
?.asString
?.trim()
?.let {
Expand Down

0 comments on commit db379cc

Please sign in to comment.