CoroutinesEventBus add.

- koin dependency injection add
- EventDispatcher fix
This commit is contained in:
dalbodeule
2024-07-31 16:14:58 +09:00
parent 99ec9ba7a0
commit 180dbc85bf
9 changed files with 105 additions and 58 deletions

View File

@@ -11,7 +11,7 @@ repositories {
dependencies {
// https://mvnrepository.com/artifact/net.dv8tion/JDA
implementation("net.dv8tion:JDA:5.0.1") {
api("net.dv8tion:JDA:5.0.1") {
exclude(module = "opus-java")
}
@@ -35,6 +35,9 @@ dependencies {
// https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp
implementation("com.squareup.okhttp3:okhttp:4.12.0")
// https://mvnrepository.com/artifact/io.insert-koin/koin-core
implementation("io.insert-koin:koin-core:4.0.0-RC1")
testImplementation(kotlin("test"))
listOf(project(":common")).forEach {

View File

@@ -1,5 +1,9 @@
package space.mori.chzzk_bot.chatbot.chzzk
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import space.mori.chzzk_bot.chatbot.chzzk.Connector.chzzk
@@ -138,11 +142,14 @@ class UserHandler(
logger.info("ChzzkChat connecting... ${channel.channelName} - ${channel.channelId}")
listener.connectBlocking()
Discord.sendDiscord(user, status)
streamStartTime = LocalDateTime.now()
listener.sendChat("${user.username} 님의 방송이 감지되었습니다.")
CoroutineScope(Dispatchers.Default).launch {
delay(5000L)
Discord.sendDiscord(user, status)
}
} else {
logger.info("${user.username} is offline.")
streamStartTime = null

View File

@@ -1,6 +1,10 @@
package space.mori.chzzk_bot.chatbot.chzzk
import space.mori.chzzk_bot.common.events.EventDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.koin.java.KoinJavaComponent.inject
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
@@ -30,6 +34,8 @@ class MessageHandler(
private val logger = handler.logger
private val listener = handler.listener
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
init {
reloadCommand()
}
@@ -118,39 +124,53 @@ class MessageHandler(
return
}
val parts = msg.content.split("/", limit = 2)
val parts = msg.content.split(" ", limit = 2)
if (parts.size < 2) {
listener.sendChat("타이머 명령어 형식을 잘 찾아봐주세요!")
return
}
val command = parts[1]
val dispatcher = EventDispatcher
when(command) {
when (val command = parts[1]) {
"업타임" -> {
logger.debug("${user.token} / 업타임")
val currentTime = LocalDateTime.now()
val streamOnTime = handler.streamStartTime
val hours = ChronoUnit.HOURS.between(currentTime, streamOnTime)
val minutes = ChronoUnit.MINUTES.between(currentTime.plusHours(hours), streamOnTime)
val seconds = ChronoUnit.MINUTES.between(currentTime.plusHours(hours).plusMinutes(minutes), streamOnTime)
val hours = ChronoUnit.HOURS.between(streamOnTime, currentTime)
val minutes = ChronoUnit.MINUTES.between(streamOnTime?.plusHours(hours), currentTime)
val seconds = ChronoUnit.SECONDS.between(streamOnTime?.plusHours(hours)?.plusMinutes(minutes), currentTime)
dispatcher.dispatch(TimerEvent(
user.token,
TimerType.TIMER,
String.format("%02d:%02d:%02d", hours, minutes, seconds)
))
CoroutineScope(Dispatchers.Default).launch {
dispatcher.post(
TimerEvent(
user.token,
TimerType.TIMER,
String.format("%02d:%02d:%02d", hours, minutes, seconds)
)
)
}
}
"삭제" -> {
logger.debug("${user.token} / 삭제")
CoroutineScope(Dispatchers.Default).launch {
dispatcher.post(TimerEvent(user.token, TimerType.REMOVE, ""))
}
}
"삭제" -> dispatcher.dispatch(TimerEvent(user.token, TimerType.REMOVE, ""))
else -> {
logger.debug("${user.token} / 그외")
try {
val time = command.toInt()
val currentTime = LocalDateTime.now()
val timestamp = ChronoUnit.MINUTES.addTo(currentTime, time.toLong())
val timestamp = currentTime.plus(time.toLong(), ChronoUnit.MINUTES)
dispatcher.dispatch(TimerEvent(user.token, TimerType.TIMER, timestamp.toString()))
} catch(_: Exception) {
CoroutineScope(Dispatchers.Default).launch {
dispatcher.post(TimerEvent(user.token, TimerType.TIMER, timestamp.toString()))
}
} catch (e: NumberFormatException) {
listener.sendChat("!타이머/숫자 형식으로 적어주세요! 단위: 분")
} catch (e: Exception) {
listener.sendChat("타이머 설정 중 오류가 발생했습니다.")
logger.error("Error processing timer command: ${e.message}", e)
}
}
}
@@ -190,7 +210,7 @@ class MessageHandler(
}
// Replace followPattern
result = followPattern.replace(result) { matchResult ->
result = followPattern.replace(result) { _ ->
try {
val followingDate = getFollowDate(listener.chatId, msg.userId)
.content.streamingProperty.following?.followDate