add update command.

This commit is contained in:
dalbodeule 2024-06-12 23:12:59 +09:00
parent 02d12e74b5
commit 2d28bf8bcb
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
5 changed files with 78 additions and 1 deletions

View File

@ -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.Commands
import net.dv8tion.jda.api.interactions.commands.build.OptionData import net.dv8tion.jda.api.interactions.commands.build.OptionData
import org.slf4j.LoggerFactory 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.Command
import space.mori.chzzk_bot.discord.CommandInterface import space.mori.chzzk_bot.discord.CommandInterface
import space.mori.chzzk_bot.services.CommandService import space.mori.chzzk_bot.services.CommandService
@ -33,9 +35,13 @@ object AddCommand : CommandInterface {
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue() event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
return return
} }
val chzzkChannel = Connector.getChannel(user.token)
try { try {
CommandService.saveCommand(user, label, content) CommandService.saveCommand(user, label, content)
try {
ChzzkHandler.reloadCommand(chzzkChannel!!)
} catch (_: Exception) {}
event.hook.sendMessage("등록이 완료되었습니다. $label = $content").queue() event.hook.sendMessage("등록이 완료되었습니다. $label = $content").queue()
} catch (e: Exception) { } catch (e: Exception) {
event.hook.sendMessage("에러가 발생했습니다.").queue() event.hook.sendMessage("에러가 발생했습니다.").queue()

View File

@ -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.Commands
import net.dv8tion.jda.api.interactions.commands.build.OptionData import net.dv8tion.jda.api.interactions.commands.build.OptionData
import org.slf4j.LoggerFactory 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.Command
import space.mori.chzzk_bot.discord.CommandInterface import space.mori.chzzk_bot.discord.CommandInterface
import space.mori.chzzk_bot.services.CommandService import space.mori.chzzk_bot.services.CommandService
@ -31,9 +33,13 @@ object RemoveCommand : CommandInterface {
event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue() event.hook.sendMessage("치지직 계정을 찾을 수 없습니다.").queue()
return return
} }
val chzzkChannel = Connector.getChannel(user.token)
try { try {
CommandService.removeCommand(user, label) CommandService.removeCommand(user, label)
try {
ChzzkHandler.reloadCommand(chzzkChannel!!)
} catch (_: Exception) {}
event.hook.sendMessage("삭제가 완료되었습니다. $label").queue() event.hook.sendMessage("삭제가 완료되었습니다. $label").queue()
} catch (e: Exception) { } catch (e: Exception) {
event.hook.sendMessage("에러가 발생했습니다.").queue() event.hook.sendMessage("에러가 발생했습니다.").queue()

View File

@ -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())
}
}
}

View File

@ -3,6 +3,7 @@ package space.mori.chzzk_bot.services
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.transactions.transaction 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.Command
import space.mori.chzzk_bot.models.Commands import space.mori.chzzk_bot.models.Commands
import space.mori.chzzk_bot.models.User 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? { fun getCommand(id: Int): Command? {
return transaction { return transaction {
return@transaction Command.findById(id) return@transaction Command.findById(id)

View File

@ -3,7 +3,7 @@
<!-- 콘솔에 출력하는 기본 로그 설정 --> <!-- 콘솔에 출력하는 기본 로그 설정 -->
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>INFO</level> <level>DEBUG</level>
</filter> </filter>
<encoder> <encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>