From db379ccbfc9298a06dc69c53d286f13605d79095 Mon Sep 17 00:00:00 2001 From: ShaksterNano <54268387+shaksternano@users.noreply.github.com> Date: Mon, 30 Sep 2024 21:54:45 +0100 Subject: [PATCH] refactor: extract command modal and text field ids to a constant --- .../borgar/discord/command/DiscordCommands.kt | 4 ++-- .../discord/command/DiscordModalCommandHandler.kt | 11 +++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/discord/src/main/kotlin/io/github/shaksternano/borgar/discord/command/DiscordCommands.kt b/discord/src/main/kotlin/io/github/shaksternano/borgar/discord/command/DiscordCommands.kt index d018d6e5..47deb3b2 100644 --- a/discord/src/main/kotlin/io/github/shaksternano/borgar/discord/command/DiscordCommands.kt +++ b/discord/src/main/kotlin/io/github/shaksternano/borgar/discord/command/DiscordCommands.kt @@ -44,7 +44,7 @@ suspend fun JDA.registerCommands() { listener { handleCommandAutoComplete(it) } - val commandModalInteractionName = "Run command" + val commandModalInteractionName = "Run Command" listener { if (it.name == commandModalInteractionName) { createCommandModal(it) @@ -56,7 +56,7 @@ suspend fun JDA.registerCommands() { handleUserInteraction(it.convert()) } listener { - if (it.modalId == "command") { + if (it.modalId == COMMAND_MODAL_ID) { handleModalCommand(it) } else { logger.error("Unknown modal ID: ${it.modalId}") diff --git a/discord/src/main/kotlin/io/github/shaksternano/borgar/discord/command/DiscordModalCommandHandler.kt b/discord/src/main/kotlin/io/github/shaksternano/borgar/discord/command/DiscordModalCommandHandler.kt index bb7ca8e2..9f1b76cb 100644 --- a/discord/src/main/kotlin/io/github/shaksternano/borgar/discord/command/DiscordModalCommandHandler.kt +++ b/discord/src/main/kotlin/io/github/shaksternano/borgar/discord/command/DiscordModalCommandHandler.kt @@ -23,9 +23,12 @@ 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, ) { @@ -33,8 +36,8 @@ suspend fun createCommandModal(event: MessageContextInteractionEvent) { builder.minLength = 1 } val modal = Modal( - id = "command", - title = "Run command", + id = COMMAND_MODAL_ID, + title = "Run Command", ) { components += ActionRow.of(command) } @@ -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 {