mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-08 23:08:20 +00:00
Compare commits
5 Commits
ba9fb052cd
...
593b98b7fb
Author | SHA1 | Date | |
---|---|---|---|
|
593b98b7fb | ||
|
95676e9b39 | ||
|
722b5972d9 | ||
|
a88f994ccd | ||
|
49541f7289 |
@ -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,8 +81,10 @@ object ChzzkHandler {
|
||||
}
|
||||
|
||||
fun disable() {
|
||||
handlers.forEach { handler ->
|
||||
handler.disable()
|
||||
CoroutineScope(Dispatchers.Default).launch {
|
||||
handlers.forEach { handler ->
|
||||
handler.disable()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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,57 +216,53 @@ 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)
|
||||
|
||||
client.loginAsync().join()
|
||||
listener = ChzzkSessionBuilder(client).buildUserSession()
|
||||
listener.createAndConnectAsync().join()
|
||||
messageHandler = MessageHandler(this@UserHandler)
|
||||
|
||||
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(
|
||||
TimerEvent(
|
||||
channel.channelId,
|
||||
TimerType.UPTIME,
|
||||
getUptime(streamStartTime!!)
|
||||
)
|
||||
)
|
||||
else dispatcher.post(
|
||||
TimerEvent(
|
||||
channel.channelId,
|
||||
TimerType.entries.firstOrNull { it.value == timer?.option } ?: TimerType.REMOVE,
|
||||
null
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
} catch(e: Exception) {
|
||||
logger.error("Exception(${user.username}): ${e.stackTraceToString()}")
|
||||
throw RuntimeException("Exception: ${e.stackTraceToString()}")
|
||||
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) ?: throw RuntimeException("Chat Channel ID is not found.")
|
||||
|
||||
client.loginAsync().await()
|
||||
listener = ChzzkSessionBuilder(client).buildUserSession()
|
||||
listener.createAndConnectAsync().await()
|
||||
|
||||
delay(1000L)
|
||||
|
||||
listener.on(SessionChatMessageEvent::class.java) {
|
||||
messageHandler.handle(it.message, user)
|
||||
}
|
||||
|
||||
val timer = TimerConfigService.getConfig(user)
|
||||
if (timer?.option == TimerType.UPTIME.value)
|
||||
dispatcher.post(
|
||||
TimerEvent(
|
||||
channel.channelId,
|
||||
TimerType.UPTIME,
|
||||
getUptime(streamStartTime!!)
|
||||
)
|
||||
)
|
||||
else dispatcher.post(
|
||||
TimerEvent(
|
||||
channel.channelId,
|
||||
TimerType.entries.firstOrNull { it.value == timer?.option } ?: TimerType.REMOVE,
|
||||
null
|
||||
)
|
||||
)
|
||||
|
||||
messageHandler = MessageHandler(this@UserHandler)
|
||||
}
|
||||
|
||||
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)!!)
|
||||
|
Loading…
x
Reference in New Issue
Block a user