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