Compare commits

...

5 Commits

Author SHA1 Message Date
dalbodeule
593b98b7fb
[hotfix] coroutine related fixed. (4x) 2025-06-08 15:36:33 +09:00
dalbodeule
95676e9b39
[hotfix] coroutine related fixed. (3x) 2025-06-08 15:28:50 +09:00
dalbodeule
722b5972d9
[hotfix] coroutine related fixed. (2x) 2025-06-08 15:24:33 +09:00
dalbodeule
a88f994ccd
[hotfix] coroutine related fixed. 2025-06-08 15:10:41 +09:00
dalbodeule
49541f7289
[hotfix] connect function added. 2025-06-08 15:03:35 +09:00

View File

@ -1,11 +1,7 @@
package space.mori.chzzk_bot.chatbot.chzzk package space.mori.chzzk_bot.chatbot.chzzk
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.*
import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.future.await
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.koin.java.KoinJavaComponent.inject import org.koin.java.KoinJavaComponent.inject
import org.slf4j.Logger import org.slf4j.Logger
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
@ -17,7 +13,8 @@ import space.mori.chzzk_bot.common.models.User
import space.mori.chzzk_bot.common.services.LiveStatusService import space.mori.chzzk_bot.common.services.LiveStatusService
import space.mori.chzzk_bot.common.services.TimerConfigService 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.* import space.mori.chzzk_bot.common.utils.getChzzkChannelId
import space.mori.chzzk_bot.common.utils.getUptime
import xyz.r2turntrue.chzzk4j.ChzzkClient import xyz.r2turntrue.chzzk4j.ChzzkClient
import xyz.r2turntrue.chzzk4j.session.ChzzkSessionBuilder import xyz.r2turntrue.chzzk4j.session.ChzzkSessionBuilder
import xyz.r2turntrue.chzzk4j.session.ChzzkSessionSubscriptionType import xyz.r2turntrue.chzzk4j.session.ChzzkSessionSubscriptionType
@ -25,10 +22,11 @@ import xyz.r2turntrue.chzzk4j.session.ChzzkUserSession
import xyz.r2turntrue.chzzk4j.session.event.SessionChatMessageEvent import xyz.r2turntrue.chzzk4j.session.event.SessionChatMessageEvent
import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveDetail import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveDetail
import java.lang.Exception import java.lang.Runnable
import java.lang.Thread
import java.net.SocketTimeoutException import java.net.SocketTimeoutException
import java.time.LocalDateTime
import java.nio.charset.Charset import java.nio.charset.Charset
import java.time.LocalDateTime
object ChzzkHandler { object ChzzkHandler {
private val handlers = mutableListOf<UserHandler>() private val handlers = mutableListOf<UserHandler>()
@ -83,10 +81,12 @@ object ChzzkHandler {
} }
fun disable() { fun disable() {
CoroutineScope(Dispatchers.Default).launch {
handlers.forEach { handler -> handlers.forEach { handler ->
handler.disable() handler.disable()
} }
} }
}
internal fun reloadCommand(chzzkChannel: ChzzkChannel) { internal fun reloadCommand(chzzkChannel: ChzzkChannel) {
val handler = handlers.firstOrNull { it.channel.channelId == chzzkChannel.channelId } val handler = handlers.firstOrNull { it.channel.channelId == chzzkChannel.channelId }
@ -118,7 +118,7 @@ object ChzzkHandler {
try { try {
it.isActive(true, streamInfo) it.isActive(true, streamInfo)
} catch(e: Exception) { } catch(e: Exception) {
logger.info("Exception: ${e.stackTraceToString()}") logger.info("Thread 1 Exception: ${e.stackTraceToString()}")
} }
} }
if (streamInfo?.isOnline == false && it.isActive) it.isActive(false, streamInfo) if (streamInfo?.isOnline == false && it.isActive) it.isActive(false, streamInfo)
@ -146,14 +146,14 @@ object ChzzkHandler {
try { try {
it.isActive(true, streamInfo) it.isActive(true, streamInfo)
} catch(e: Exception) { } catch(e: Exception) {
logger.info("Exception: ${e.stackTraceToString()}") logger.info("Thread 2 Exception: ${e.stackTraceToString()}")
} }
} }
if (streamInfo?.isOnline == false && it.isActive) it.isActive(false, streamInfo) if (streamInfo?.isOnline == false && it.isActive) it.isActive(false, streamInfo)
} catch (e: SocketTimeoutException) { } catch (e: SocketTimeoutException) {
logger.info("Thread 1 Timeout: ${it.channel.channelName} / ${e.stackTraceToString()}") logger.info("Thread 2 Timeout: ${it.channel.channelName} / ${e.stackTraceToString()}")
} catch (e: Exception) { } catch (e: Exception) {
logger.info("Thread 1 Exception: ${it.channel.channelName} / ${e.stackTraceToString()}") logger.info("Thread 2 Exception: ${it.channel.channelName} / ${e.stackTraceToString()}")
} finally { } finally {
Thread.sleep(5000) Thread.sleep(5000)
} }
@ -204,10 +204,10 @@ class UserHandler(
private var user: User, private var user: User,
var streamStartTime: LocalDateTime?, var streamStartTime: LocalDateTime?,
) { ) {
var messageHandler: MessageHandler lateinit var messageHandler: MessageHandler
var client: ChzzkClient lateinit var client: ChzzkClient
var listener: ChzzkUserSession lateinit var listener: ChzzkUserSession
var chatChannelId: String? lateinit var chatChannelId: String
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java) private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
private var _isActive: Boolean private var _isActive: Boolean
@ -216,31 +216,31 @@ class UserHandler(
LiveStatusService.updateOrCreate(user, value) LiveStatusService.updateOrCreate(user, value)
} }
init { private suspend fun connect() {
val user = UserService.getUser(channel.channelId) val user = UserService.getUser(channel.channelId)
if(user?.accessToken == null || user.refreshToken == null) { if(user?.accessToken == null || user.refreshToken == null) {
throw RuntimeException("AccessToken or RefreshToken is not valid.") throw RuntimeException("AccessToken or RefreshToken is not valid.")
} }
try {
val tokens = user.refreshToken?.let { token -> Connector.client.refreshAccessToken(token)} val tokens = user.refreshToken?.let { token -> Connector.client.refreshAccessToken(token)}
if(tokens == null) { if(tokens == null) {
throw RuntimeException("AccessToken is not valid.") throw RuntimeException("AccessToken is not valid.")
} }
client = Connector.getClient(tokens.first, tokens.second) client = Connector.getClient(tokens.first, tokens.second)
UserService.setRefreshToken(user, tokens.first, tokens.second) UserService.setRefreshToken(user, tokens.first, tokens.second)
chatChannelId = getChzzkChannelId(channel.channelId) chatChannelId = getChzzkChannelId(channel.channelId) ?: throw RuntimeException("Chat Channel ID is not found.")
client.loginAsync().join() client.loginAsync().await()
listener = ChzzkSessionBuilder(client).buildUserSession() listener = ChzzkSessionBuilder(client).buildUserSession()
listener.createAndConnectAsync().join() listener.createAndConnectAsync().await()
messageHandler = MessageHandler(this@UserHandler)
delay(1000L)
listener.on(SessionChatMessageEvent::class.java) { listener.on(SessionChatMessageEvent::class.java) {
messageHandler.handle(it.message, user) messageHandler.handle(it.message, user)
} }
GlobalScope.launch {
val timer = TimerConfigService.getConfig(user) val timer = TimerConfigService.getConfig(user)
if (timer?.option == TimerType.UPTIME.value) if (timer?.option == TimerType.UPTIME.value)
dispatcher.post( dispatcher.post(
@ -257,16 +257,12 @@ class UserHandler(
null null
) )
) )
messageHandler = MessageHandler(this@UserHandler)
} }
} catch(e: Exception) { internal suspend fun disable() {
logger.error("Exception(${user.username}): ${e.stackTraceToString()}") listener.disconnectAsync().await()
throw RuntimeException("Exception: ${e.stackTraceToString()}")
}
}
internal fun disable() {
listener.disconnectAsync().join()
_isActive = false _isActive = false
} }
@ -284,6 +280,8 @@ class UserHandler(
internal fun isActive(value: Boolean, status: ChzzkLiveDetail) { internal fun isActive(value: Boolean, status: ChzzkLiveDetail) {
if(value) { if(value) {
CoroutineScope(Dispatchers.Default).launch { CoroutineScope(Dispatchers.Default).launch {
connect()
logger.info("${user.username} is live.") logger.info("${user.username} is live.")
reloadUser(UserService.getUser(user.id.value)!!) reloadUser(UserService.getUser(user.id.value)!!)