From 02d12e74b52a37e7858aaffe840d07c81073a041 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Wed, 12 Jun 2024 22:59:27 +0900 Subject: [PATCH] add remove command --- .../discord/commands/RemoveCommand.kt | 43 +++++++++++++++++++ .../mori/chzzk_bot/services/CommandService.kt | 12 ++++++ 2 files changed, 55 insertions(+) create mode 100644 src/main/kotlin/space/mori/chzzk_bot/discord/commands/RemoveCommand.kt diff --git a/src/main/kotlin/space/mori/chzzk_bot/discord/commands/RemoveCommand.kt b/src/main/kotlin/space/mori/chzzk_bot/discord/commands/RemoveCommand.kt new file mode 100644 index 0000000..d6d4ecf --- /dev/null +++ b/src/main/kotlin/space/mori/chzzk_bot/discord/commands/RemoveCommand.kt @@ -0,0 +1,43 @@ +package space.mori.chzzk_bot.discord.commands + +import net.dv8tion.jda.api.JDA +import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent +import net.dv8tion.jda.api.interactions.commands.OptionType +import net.dv8tion.jda.api.interactions.commands.build.Commands +import net.dv8tion.jda.api.interactions.commands.build.OptionData +import org.slf4j.LoggerFactory +import space.mori.chzzk_bot.discord.Command +import space.mori.chzzk_bot.discord.CommandInterface +import space.mori.chzzk_bot.services.CommandService +import space.mori.chzzk_bot.services.UserService + +@Command +object RemoveCommand : CommandInterface { + private val logger = LoggerFactory.getLogger(this::class.java) + override val name: String = "remove" + override val command = Commands.slash(name, "명령어를 삭제합니다.") + .addOptions(OptionData(OptionType.STRING, "label", "삭제할 명령어를 입력하세요.", true)) + + override fun run(event: SlashCommandInteractionEvent, bot: JDA) { + val label = event.getOption("label")?.asString + + if(label == null) { + event.hook.sendMessage("명령어는 필수 입력입니다.").queue() + return + } + + val user = UserService.getUser(event.user.idLong) + if(user == null) { + event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue() + return + } + + try { + CommandService.removeCommand(user, label) + event.hook.sendMessage("삭제가 완료되었습니다. $label").queue() + } catch (e: Exception) { + event.hook.sendMessage("에러가 발생했습니다.").queue() + logger.debug(e.stackTraceToString()) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/space/mori/chzzk_bot/services/CommandService.kt b/src/main/kotlin/space/mori/chzzk_bot/services/CommandService.kt index 0e3b4ea..8461aac 100644 --- a/src/main/kotlin/space/mori/chzzk_bot/services/CommandService.kt +++ b/src/main/kotlin/space/mori/chzzk_bot/services/CommandService.kt @@ -1,6 +1,7 @@ package space.mori.chzzk_bot.services import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.transactions.transaction import space.mori.chzzk_bot.models.Command import space.mori.chzzk_bot.models.Commands @@ -17,6 +18,17 @@ object CommandService { } } + fun removeCommand(user: User, command: String): Command? { + return transaction { + val commandRow = Command.find(Commands.user eq user.id and(Commands.command eq command)).firstOrNull() + + commandRow ?: throw RuntimeException("Command not found! $command") + commandRow.delete() + + return@transaction commandRow + } + } + fun getCommand(id: Int): Command? { return transaction { return@transaction Command.findById(id)