From f7c68a56bce23b43b7467596a403e78badf0a812 Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Sat, 3 Aug 2024 13:57:03 +0900 Subject: [PATCH] add TimerConfig.kt, TimerConfigService --- .../chzzk_bot/chatbot/chzzk/ChzzkHandler.kt | 19 ++++++-- .../chzzk_bot/chatbot/chzzk/MessageHandler.kt | 16 ++++++- .../chzzk_bot/common/models/TimerConfig.kt | 18 +++++++ .../common/services/CommandService.kt | 2 +- .../common/services/TimerConfigService.kt | 48 +++++++++++++++++++ 5 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 common/src/main/kotlin/space/mori/chzzk_bot/common/models/TimerConfig.kt create mode 100644 common/src/main/kotlin/space/mori/chzzk_bot/common/services/TimerConfigService.kt diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt index 574ddf8..7b001c8 100644 --- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt +++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt @@ -13,6 +13,7 @@ import space.mori.chzzk_bot.common.events.CoroutinesEventBus import space.mori.chzzk_bot.common.events.TimerEvent import space.mori.chzzk_bot.common.events.TimerType import space.mori.chzzk_bot.common.models.User +import space.mori.chzzk_bot.common.services.TimerConfigService import space.mori.chzzk_bot.common.services.UserService import space.mori.chzzk_bot.common.utils.convertChzzkDateToLocalDateTime import space.mori.chzzk_bot.common.utils.getUptime @@ -152,11 +153,19 @@ class UserHandler( streamStartTime = convertChzzkDateToLocalDateTime(status.content.openDate) CoroutineScope(Dispatchers.Default).launch { - dispatcher.post(TimerEvent( - channel.channelId, - TimerType.UPTIME, - getUptime(streamStartTime!!) - )) + when(TimerConfigService.getConfig(UserService.getUser(channel.channelId)!!)?.option) { + TimerType.UPTIME.value -> dispatcher.post(TimerEvent( + channel.channelId, + TimerType.UPTIME, + getUptime(streamStartTime!!) + )) + else -> dispatcher.post(TimerEvent( + channel.channelId, + TimerType.REMOVE, + "" + )) + } + delay(5000L) listener.sendChat("${user.username} 님! 오늘도 열심히 방송하세요!") Discord.sendDiscord(user, status) diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt index 86e3d38..c0ef401 100644 --- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt +++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/MessageHandler.kt @@ -10,6 +10,7 @@ import space.mori.chzzk_bot.common.events.TimerType import space.mori.chzzk_bot.common.models.User import space.mori.chzzk_bot.common.services.CommandService import space.mori.chzzk_bot.common.services.CounterService +import space.mori.chzzk_bot.common.services.TimerConfigService import space.mori.chzzk_bot.common.services.UserService import space.mori.chzzk_bot.common.utils.getUptime import xyz.r2turntrue.chzzk4j.chat.ChatMessage @@ -125,7 +126,7 @@ class MessageHandler( return } - val parts = msg.content.split(" ", limit = 2) + val parts = msg.content.split(" ", limit = 3) if (parts.size < 2) { listener.sendChat("타이머 명령어 형식을 잘 찾아봐주세요!") return @@ -152,6 +153,19 @@ class MessageHandler( dispatcher.post(TimerEvent(user.token, TimerType.REMOVE, "")) } } + "설정" -> { + when (parts[2]) { + "업타임" -> { + TimerConfigService.saveOrUpdateConfig(user, TimerType.UPTIME) + listener.sendChat("기본 타이머 설정이 업타임으로 바뀌었습니다.") + } + "삭제" -> { + TimerConfigService.saveOrUpdateConfig(user, TimerType.REMOVE) + listener.sendChat("기본 타이머 설정이 삭제로 바뀌었습니다.") + } + else -> listener.sendChat("!타이머 설정 (업타임/삭제) 형식으로 써주세요!") + } + } else -> { logger.debug("${user.token} / 그외") try { diff --git a/common/src/main/kotlin/space/mori/chzzk_bot/common/models/TimerConfig.kt b/common/src/main/kotlin/space/mori/chzzk_bot/common/models/TimerConfig.kt new file mode 100644 index 0000000..787e990 --- /dev/null +++ b/common/src/main/kotlin/space/mori/chzzk_bot/common/models/TimerConfig.kt @@ -0,0 +1,18 @@ +package space.mori.chzzk_bot.common.models + +import org.jetbrains.exposed.dao.IntEntity +import org.jetbrains.exposed.dao.IntEntityClass +import org.jetbrains.exposed.dao.id.EntityID +import org.jetbrains.exposed.dao.id.IntIdTable +import org.jetbrains.exposed.sql.ReferenceOption + +object TimerConfigs: IntIdTable("timer_config") { + val user = reference("user", Users, onDelete = ReferenceOption.CASCADE) + val option = integer("option") +} +class TimerConfig(id: EntityID) : IntEntity(id) { + companion object : IntEntityClass(TimerConfigs) + + var user by User referencedOn TimerConfigs.user + var option by TimerConfigs.option +} \ No newline at end of file diff --git a/common/src/main/kotlin/space/mori/chzzk_bot/common/services/CommandService.kt b/common/src/main/kotlin/space/mori/chzzk_bot/common/services/CommandService.kt index 3dc4c49..fe9598f 100644 --- a/common/src/main/kotlin/space/mori/chzzk_bot/common/services/CommandService.kt +++ b/common/src/main/kotlin/space/mori/chzzk_bot/common/services/CommandService.kt @@ -11,7 +11,7 @@ import space.mori.chzzk_bot.common.models.User object CommandService { fun saveCommand(user: User, command: String, content: String, failContent: String): Command { return transaction { - return@transaction Command.new { + Command.new { this.user = user this.command = command this.content = content diff --git a/common/src/main/kotlin/space/mori/chzzk_bot/common/services/TimerConfigService.kt b/common/src/main/kotlin/space/mori/chzzk_bot/common/services/TimerConfigService.kt new file mode 100644 index 0000000..9fc2eb9 --- /dev/null +++ b/common/src/main/kotlin/space/mori/chzzk_bot/common/services/TimerConfigService.kt @@ -0,0 +1,48 @@ +package space.mori.chzzk_bot.common.services + +import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.transactions.transaction +import org.jetbrains.exposed.sql.update +import space.mori.chzzk_bot.common.events.TimerType +import space.mori.chzzk_bot.common.models.TimerConfig +import space.mori.chzzk_bot.common.models.TimerConfigs +import space.mori.chzzk_bot.common.models.User + +object TimerConfigService { + fun saveConfig(user: User, timerConfig: TimerType) { + return transaction { + TimerConfig.new { + this.user = user + this.option = timerConfig.value + } + } + } + + fun updateConfig(user: User, timerConfig: TimerType) { + return transaction { + val updated = TimerConfigs.update({ + TimerConfigs.user eq user.id + }) { + it[option] = timerConfig.value + } + + if (updated == 0) throw RuntimeException("TimerConfig not found! ${user.username}") + + TimerConfig.find { TimerConfigs.user eq user.id }.first() + } + } + + fun getConfig(user: User): TimerConfig? { + return transaction { + TimerConfig.find(TimerConfigs.user eq user.id).firstOrNull() + } + } + + fun saveOrUpdateConfig(user: User, timerConfig: TimerType) { + return if (getConfig(user) == null) { + saveConfig(user, timerConfig) + } else { + updateConfig(user, timerConfig) + } + } +} \ No newline at end of file