add TimerConfig.kt, TimerConfigService

This commit is contained in:
dalbodeule 2024-08-03 13:57:03 +09:00
parent 55f6f5f94d
commit f7c68a56bc
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
5 changed files with 96 additions and 7 deletions

View File

@ -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.TimerEvent
import space.mori.chzzk_bot.common.events.TimerType import space.mori.chzzk_bot.common.events.TimerType
import space.mori.chzzk_bot.common.models.User 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.services.UserService
import space.mori.chzzk_bot.common.utils.convertChzzkDateToLocalDateTime import space.mori.chzzk_bot.common.utils.convertChzzkDateToLocalDateTime
import space.mori.chzzk_bot.common.utils.getUptime import space.mori.chzzk_bot.common.utils.getUptime
@ -152,11 +153,19 @@ class UserHandler(
streamStartTime = convertChzzkDateToLocalDateTime(status.content.openDate) streamStartTime = convertChzzkDateToLocalDateTime(status.content.openDate)
CoroutineScope(Dispatchers.Default).launch { CoroutineScope(Dispatchers.Default).launch {
dispatcher.post(TimerEvent( when(TimerConfigService.getConfig(UserService.getUser(channel.channelId)!!)?.option) {
TimerType.UPTIME.value -> dispatcher.post(TimerEvent(
channel.channelId, channel.channelId,
TimerType.UPTIME, TimerType.UPTIME,
getUptime(streamStartTime!!) getUptime(streamStartTime!!)
)) ))
else -> dispatcher.post(TimerEvent(
channel.channelId,
TimerType.REMOVE,
""
))
}
delay(5000L) delay(5000L)
listener.sendChat("${user.username} 님! 오늘도 열심히 방송하세요!") listener.sendChat("${user.username} 님! 오늘도 열심히 방송하세요!")
Discord.sendDiscord(user, status) Discord.sendDiscord(user, status)

View File

@ -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.models.User
import space.mori.chzzk_bot.common.services.CommandService import space.mori.chzzk_bot.common.services.CommandService
import space.mori.chzzk_bot.common.services.CounterService 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.services.UserService
import space.mori.chzzk_bot.common.utils.getUptime import space.mori.chzzk_bot.common.utils.getUptime
import xyz.r2turntrue.chzzk4j.chat.ChatMessage import xyz.r2turntrue.chzzk4j.chat.ChatMessage
@ -125,7 +126,7 @@ class MessageHandler(
return return
} }
val parts = msg.content.split(" ", limit = 2) val parts = msg.content.split(" ", limit = 3)
if (parts.size < 2) { if (parts.size < 2) {
listener.sendChat("타이머 명령어 형식을 잘 찾아봐주세요!") listener.sendChat("타이머 명령어 형식을 잘 찾아봐주세요!")
return return
@ -152,6 +153,19 @@ class MessageHandler(
dispatcher.post(TimerEvent(user.token, TimerType.REMOVE, "")) 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 -> { else -> {
logger.debug("${user.token} / 그외") logger.debug("${user.token} / 그외")
try { try {

View File

@ -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<Int>) : IntEntity(id) {
companion object : IntEntityClass<TimerConfig>(TimerConfigs)
var user by User referencedOn TimerConfigs.user
var option by TimerConfigs.option
}

View File

@ -11,7 +11,7 @@ import space.mori.chzzk_bot.common.models.User
object CommandService { object CommandService {
fun saveCommand(user: User, command: String, content: String, failContent: String): Command { fun saveCommand(user: User, command: String, content: String, failContent: String): Command {
return transaction { return transaction {
return@transaction Command.new { Command.new {
this.user = user this.user = user
this.command = command this.command = command
this.content = content this.content = content

View File

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