some change on TimerEvents.

This commit is contained in:
dalbodeule 2024-07-30 23:00:29 +09:00
parent a9ee40e936
commit 99ec9ba7a0
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65
3 changed files with 11 additions and 6 deletions

View File

@ -38,7 +38,7 @@ class MessageHandler(
val user = UserService.getUser(channel.channelId) val user = UserService.getUser(channel.channelId)
?: throw RuntimeException("User not found. it's bug? ${channel.channelName} - ${channel.channelId}") ?: throw RuntimeException("User not found. it's bug? ${channel.channelName} - ${channel.channelId}")
val commands = CommandService.getCommands(user) val commands = CommandService.getCommands(user)
val manageCommands = mapOf("!명령어추가" to this::manageAddCommand, "!명령어삭제" to this::manageRemoveCommand, "!명령어수정" to this::manageUpdateCommand) val manageCommands = mapOf("!명령어추가" to this::manageAddCommand, "!명령어삭제" to this::manageRemoveCommand, "!명령어수정" to this::manageUpdateCommand, "!시간" to this::timerCommand)
manageCommands.forEach { (commandName, command) -> manageCommands.forEach { (commandName, command) ->
this.commands[commandName] = command this.commands[commandName] = command
@ -112,7 +112,7 @@ class MessageHandler(
listener.sendChat("명령어 '$command' 삭제되었습니다.") listener.sendChat("명령어 '$command' 삭제되었습니다.")
} }
private suspend fun timerCommand(msg: ChatMessage, user: User) { private fun timerCommand(msg: ChatMessage, user: User) {
if (msg.profile?.userRoleCode == "common_user") { if (msg.profile?.userRoleCode == "common_user") {
listener.sendChat("매니저만 이 명령어를 사용할 수 있습니다.") listener.sendChat("매니저만 이 명령어를 사용할 수 있습니다.")
return return

View File

@ -3,7 +3,7 @@ package space.mori.chzzk_bot.common.events
interface Event interface Event
interface EventHandler<E: Event> { interface EventHandler<E: Event> {
suspend fun handle(event: E) fun handle(event: E)
} }
object EventDispatcher { object EventDispatcher {
@ -13,7 +13,7 @@ object EventDispatcher {
handlers.computeIfAbsent(eventClass) { mutableListOf() }.add(handler) handlers.computeIfAbsent(eventClass) { mutableListOf() }.add(handler)
} }
suspend fun <E : Event> dispatch(event: E) { fun <E : Event> dispatch(event: E) {
handlers[event::class.java]?.forEach { (it as EventHandler<E>).handle(event) } handlers[event::class.java]?.forEach { (it as EventHandler<E>).handle(event) }
} }
} }

View File

@ -3,7 +3,10 @@ package space.mori.chzzk_bot.webserver.routes
import io.ktor.server.routing.* import io.ktor.server.routing.*
import io.ktor.server.websocket.* import io.ktor.server.websocket.*
import io.ktor.websocket.* import io.ktor.websocket.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.ClosedReceiveChannelException import kotlinx.coroutines.channels.ClosedReceiveChannelException
import kotlinx.coroutines.launch
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable
import space.mori.chzzk_bot.common.events.Event import space.mori.chzzk_bot.common.events.Event
import space.mori.chzzk_bot.common.events.TimerType import space.mori.chzzk_bot.common.events.TimerType
@ -52,9 +55,11 @@ fun Routing.wsTimerRoutes() {
val dispatcher = EventDispatcher val dispatcher = EventDispatcher
dispatcher.register(TimerEvent::class.java, object : EventHandler<TimerEvent> { dispatcher.register(TimerEvent::class.java, object : EventHandler<TimerEvent> {
override suspend fun handle(event: TimerEvent) { override fun handle(event: TimerEvent) {
CoroutineScope(Dispatchers.IO).launch {
sessions[event.uid]?.sendSerialized(TimerResponse(event.type, event.time ?: "")) sessions[event.uid]?.sendSerialized(TimerResponse(event.type, event.time ?: ""))
} }
}
}) })
} }
} }