From 2d28bf8bcbe1a418423ed0cbea10cc04641158d4 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Wed, 12 Jun 2024 23:12:59 +0900 Subject: [PATCH] add update command. --- .../chzzk_bot/discord/commands/AddCommand.kt | 6 +++ .../discord/commands/RemoveCommand.kt | 6 +++ .../discord/commands/UpdateCommand.kt | 52 +++++++++++++++++++ .../mori/chzzk_bot/services/CommandService.kt | 13 +++++ src/main/resources/logback.xml | 2 +- 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 src/main/kotlin/space/mori/chzzk_bot/discord/commands/UpdateCommand.kt diff --git a/src/main/kotlin/space/mori/chzzk_bot/discord/commands/AddCommand.kt b/src/main/kotlin/space/mori/chzzk_bot/discord/commands/AddCommand.kt index 2a079dc..c25ffcd 100644 --- a/src/main/kotlin/space/mori/chzzk_bot/discord/commands/AddCommand.kt +++ b/src/main/kotlin/space/mori/chzzk_bot/discord/commands/AddCommand.kt @@ -6,6 +6,8 @@ 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.chzzk.ChzzkHandler +import space.mori.chzzk_bot.chzzk.Connector import space.mori.chzzk_bot.discord.Command import space.mori.chzzk_bot.discord.CommandInterface import space.mori.chzzk_bot.services.CommandService @@ -33,9 +35,13 @@ object AddCommand : CommandInterface { event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue() return } + val chzzkChannel = Connector.getChannel(user.token) try { CommandService.saveCommand(user, label, content) + try { + ChzzkHandler.reloadCommand(chzzkChannel!!) + } catch (_: Exception) {} event.hook.sendMessage("등록이 완료되었습니다. $label = $content").queue() } catch (e: Exception) { event.hook.sendMessage("에러가 발생했습니다.").queue() 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 index d6d4ecf..9d520ee 100644 --- a/src/main/kotlin/space/mori/chzzk_bot/discord/commands/RemoveCommand.kt +++ b/src/main/kotlin/space/mori/chzzk_bot/discord/commands/RemoveCommand.kt @@ -6,6 +6,8 @@ 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.chzzk.ChzzkHandler +import space.mori.chzzk_bot.chzzk.Connector import space.mori.chzzk_bot.discord.Command import space.mori.chzzk_bot.discord.CommandInterface import space.mori.chzzk_bot.services.CommandService @@ -31,9 +33,13 @@ object RemoveCommand : CommandInterface { event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue() return } + val chzzkChannel = Connector.getChannel(user.token) try { CommandService.removeCommand(user, label) + try { + ChzzkHandler.reloadCommand(chzzkChannel!!) + } catch (_: Exception) {} event.hook.sendMessage("삭제가 완료되었습니다. $label").queue() } catch (e: Exception) { event.hook.sendMessage("에러가 발생했습니다.").queue() diff --git a/src/main/kotlin/space/mori/chzzk_bot/discord/commands/UpdateCommand.kt b/src/main/kotlin/space/mori/chzzk_bot/discord/commands/UpdateCommand.kt new file mode 100644 index 0000000..da30620 --- /dev/null +++ b/src/main/kotlin/space/mori/chzzk_bot/discord/commands/UpdateCommand.kt @@ -0,0 +1,52 @@ +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.chzzk.ChzzkHandler +import space.mori.chzzk_bot.chzzk.Connector +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 +import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel + +@Command +object UpdateCommand : CommandInterface { + private val logger = LoggerFactory.getLogger(this::class.java) + override val name: String = "update" + override val command = Commands.slash(name, "명령어를 수정합니다.") + .addOptions(OptionData(OptionType.STRING, "label", "수정할 명령어를 입력하세요.", true)) + .addOptions(OptionData(OptionType.STRING, "content", "표시될 텍스트를 입력하세요.", true)) + + override fun run(event: SlashCommandInteractionEvent, bot: JDA) { + val label = event.getOption("label")?.asString + val content = event.getOption("content")?.asString + + if(label == null || content == null) { + event.hook.sendMessage("명령어와 텍스트는 필수 입력입니다.").queue() + return + } + + val user = UserService.getUser(event.user.idLong) + if(user == null) { + event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue() + return + } + val chzzkChannel = Connector.getChannel(user.token) + + try { + CommandService.updateCommand(user, label, content) + try { + ChzzkHandler.reloadCommand(chzzkChannel!!) + } catch (_: Exception) {} + event.hook.sendMessage("등록이 완료되었습니다. $label = $content").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 8461aac..444514f 100644 --- a/src/main/kotlin/space/mori/chzzk_bot/services/CommandService.kt +++ b/src/main/kotlin/space/mori/chzzk_bot/services/CommandService.kt @@ -3,6 +3,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 org.jetbrains.exposed.sql.update import space.mori.chzzk_bot.models.Command import space.mori.chzzk_bot.models.Commands import space.mori.chzzk_bot.models.User @@ -29,6 +30,18 @@ object CommandService { } } + fun updateCommand(user: User, command: String, content: String): Command { + return transaction { + val updated = Commands.update({Commands.user eq user.id and(Commands.command eq command)}) { + it[Commands.content] = content + } + + if(updated == 0) throw RuntimeException("Command not found! $command") + + return@transaction Command.find(Commands.user eq user.id and(Commands.command eq command)).first() + } + } + fun getCommand(id: Int): Command? { return transaction { return@transaction Command.findById(id) diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index 25555da..f049712 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -3,7 +3,7 @@ - INFO + DEBUG %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n