mirror of
https://github.com/dalbodeule/chibot-chzzk-bot.git
synced 2025-06-09 07:18:22 +00:00
Compare commits
No commits in common. "27810c0b7f482ed52defb79e6270e5b95d2638f5" and "0709b8f526f5bdab499a5c45b0c9bd82323cd7ab" have entirely different histories.
27810c0b7f
...
0709b8f526
@ -23,7 +23,7 @@ import xyz.r2turntrue.chzzk4j.session.ChzzkSessionSubscriptionType
|
|||||||
import xyz.r2turntrue.chzzk4j.session.ChzzkUserSession
|
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.ChzzkLiveStatus
|
||||||
import java.lang.Exception
|
import java.lang.Exception
|
||||||
import java.net.SocketTimeoutException
|
import java.net.SocketTimeoutException
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
@ -37,7 +37,7 @@ object ChzzkHandler {
|
|||||||
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
fun addUser(chzzkChannel: ChzzkChannel, user: User) {
|
fun addUser(chzzkChannel: ChzzkChannel, user: User) {
|
||||||
handlers.add(UserHandler(chzzkChannel, logger, user, streamStartTime = LocalDateTime.now()))
|
handlers.add(UserHandler(chzzkChannel, logger, user, streamStartTime = null))
|
||||||
}
|
}
|
||||||
|
|
||||||
fun enable() {
|
fun enable() {
|
||||||
@ -196,7 +196,6 @@ object ChzzkHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@OptIn(DelicateCoroutinesApi::class)
|
|
||||||
class UserHandler(
|
class UserHandler(
|
||||||
val channel: ChzzkChannel,
|
val channel: ChzzkChannel,
|
||||||
val logger: Logger,
|
val logger: Logger,
|
||||||
@ -238,26 +237,6 @@ class UserHandler(
|
|||||||
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)
|
|
||||||
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) {
|
} catch(e: Exception) {
|
||||||
logger.error("Exception(${user.username}): ${e.stackTraceToString()}")
|
logger.error("Exception(${user.username}): ${e.stackTraceToString()}")
|
||||||
throw RuntimeException("Exception: ${e.stackTraceToString()}")
|
throw RuntimeException("Exception: ${e.stackTraceToString()}")
|
||||||
@ -280,7 +259,7 @@ class UserHandler(
|
|||||||
internal val isActive: Boolean
|
internal val isActive: Boolean
|
||||||
get() = _isActive
|
get() = _isActive
|
||||||
|
|
||||||
internal fun isActive(value: Boolean, status: ChzzkLiveDetail) {
|
internal fun isActive(value: Boolean, status: ChzzkLiveStatus) {
|
||||||
if(value) {
|
if(value) {
|
||||||
CoroutineScope(Dispatchers.Default).launch {
|
CoroutineScope(Dispatchers.Default).launch {
|
||||||
logger.info("${user.username} is live.")
|
logger.info("${user.username} is live.")
|
||||||
@ -288,7 +267,7 @@ class UserHandler(
|
|||||||
reloadUser(UserService.getUser(user.id.value)!!)
|
reloadUser(UserService.getUser(user.id.value)!!)
|
||||||
|
|
||||||
logger.info("ChzzkChat connecting... ${channel.channelName} - ${channel.channelId}")
|
logger.info("ChzzkChat connecting... ${channel.channelName} - ${channel.channelId}")
|
||||||
listener.subscribeAsync(ChzzkSessionSubscriptionType.CHAT).join()
|
listener.subscribeAsync(ChzzkSessionSubscriptionType.CHAT)
|
||||||
|
|
||||||
streamStartTime = LocalDateTime.now()
|
streamStartTime = LocalDateTime.now()
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ import xyz.r2turntrue.chzzk4j.ChzzkClientBuilder
|
|||||||
import xyz.r2turntrue.chzzk4j.auth.ChzzkLegacyLoginAdapter
|
import xyz.r2turntrue.chzzk4j.auth.ChzzkLegacyLoginAdapter
|
||||||
import xyz.r2turntrue.chzzk4j.auth.ChzzkSimpleUserLoginAdapter
|
import xyz.r2turntrue.chzzk4j.auth.ChzzkSimpleUserLoginAdapter
|
||||||
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.ChzzkLiveStatus
|
||||||
import kotlin.getValue
|
import kotlin.getValue
|
||||||
|
|
||||||
val dotenv = dotenv {
|
val dotenv = dotenv {
|
||||||
@ -31,7 +31,7 @@ object Connector {
|
|||||||
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
private val dispatcher: CoroutinesEventBus by inject(CoroutinesEventBus::class.java)
|
||||||
|
|
||||||
fun getChannel(channelId: String): ChzzkChannel? = client.fetchChannel(channelId)
|
fun getChannel(channelId: String): ChzzkChannel? = client.fetchChannel(channelId)
|
||||||
fun getLive(channelId: String): ChzzkLiveDetail? = client.fetchLiveDetail(channelId)
|
fun getLive(channelId: String): ChzzkLiveStatus? = client.fetchLiveStatus(channelId)
|
||||||
|
|
||||||
init {
|
init {
|
||||||
logger.info("chzzk logged: ${client.isLoggedIn}")
|
logger.info("chzzk logged: ${client.isLoggedIn}")
|
||||||
|
@ -14,8 +14,7 @@ import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder
|
|||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import space.mori.chzzk_bot.chatbot.discord.commands.*
|
import space.mori.chzzk_bot.chatbot.discord.commands.*
|
||||||
import space.mori.chzzk_bot.common.models.User
|
import space.mori.chzzk_bot.common.models.User
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveDetail
|
import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveStatus
|
||||||
import xyz.r2turntrue.chzzk4j.types.channel.live.Resolution
|
|
||||||
import java.time.Instant
|
import java.time.Instant
|
||||||
import kotlin.jvm.optionals.getOrNull
|
import kotlin.jvm.optionals.getOrNull
|
||||||
|
|
||||||
@ -34,7 +33,7 @@ class Discord: ListenerAdapter() {
|
|||||||
return bot.getGuildById(guildId)?.getTextChannelById(channelId)
|
return bot.getGuildById(guildId)?.getTextChannelById(channelId)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun sendDiscord(user: User, status: ChzzkLiveDetail) {
|
fun sendDiscord(user: User, status: ChzzkLiveStatus) {
|
||||||
if(user.liveAlertMessage != null && user.liveAlertGuild != null && user.liveAlertChannel != null) {
|
if(user.liveAlertMessage != null && user.liveAlertGuild != null && user.liveAlertChannel != null) {
|
||||||
val channel = getChannel(user.liveAlertGuild ?: 0, user.liveAlertChannel ?: 0)
|
val channel = getChannel(user.liveAlertGuild ?: 0, user.liveAlertChannel ?: 0)
|
||||||
?: throw RuntimeException("${user.liveAlertChannel} is not valid.")
|
?: throw RuntimeException("${user.liveAlertChannel} is not valid.")
|
||||||
@ -46,14 +45,7 @@ class Discord: ListenerAdapter() {
|
|||||||
embed.setAuthor(user.username, "https://chzzk.naver.com/live/${user.token}")
|
embed.setAuthor(user.username, "https://chzzk.naver.com/live/${user.token}")
|
||||||
embed.addField("카테고리", status.liveCategoryValue, true)
|
embed.addField("카테고리", status.liveCategoryValue, true)
|
||||||
embed.addField("태그", status.tags.joinToString(", ") { it.trim() }, true)
|
embed.addField("태그", status.tags.joinToString(", ") { it.trim() }, true)
|
||||||
status.defaultThumbnailImageUrl.getOrNull()?.let { embed.setImage(it) }
|
// embed.setImage(status.)
|
||||||
?: Resolution.entries.reversed().forEach {
|
|
||||||
val thumbnail = status.getLiveImageUrl(it)
|
|
||||||
if (thumbnail != null) {
|
|
||||||
embed.setImage(thumbnail)
|
|
||||||
return@forEach
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
channel.sendMessage(
|
channel.sendMessage(
|
||||||
MessageCreateBuilder()
|
MessageCreateBuilder()
|
||||||
|
@ -37,7 +37,6 @@ fun Routing.wsSongRoutes() {
|
|||||||
fun addSession(uid: String, session: WebSocketServerSession) {
|
fun addSession(uid: String, session: WebSocketServerSession) {
|
||||||
sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session)
|
sessions.computeIfAbsent(uid) { ConcurrentLinkedQueue() }.add(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun removeSession(uid: String, session: WebSocketServerSession) {
|
fun removeSession(uid: String, session: WebSocketServerSession) {
|
||||||
sessions[uid]?.remove(session)
|
sessions[uid]?.remove(session)
|
||||||
if(sessions[uid]?.isEmpty() == true) {
|
if(sessions[uid]?.isEmpty() == true) {
|
||||||
@ -89,42 +88,22 @@ fun Routing.wsSongRoutes() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
webSocket("/song/{uid}") {
|
webSocket("/song/{uid}") {
|
||||||
logger.info("WebSocket connection attempt received")
|
|
||||||
val uid = call.parameters["uid"]
|
val uid = call.parameters["uid"]
|
||||||
val user = uid?.let { UserService.getUser(it) }
|
val user = uid?.let { UserService.getUser(it) }
|
||||||
if (uid == null || user == null) {
|
if (uid == null || user == null) {
|
||||||
logger.warn("Invalid UID: $uid")
|
|
||||||
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid UID"))
|
close(CloseReason(CloseReason.Codes.CANNOT_ACCEPT, "Invalid UID"))
|
||||||
return@webSocket
|
return@webSocket
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
addSession(uid, this)
|
addSession(uid, this)
|
||||||
logger.info("WebSocket connection established for user: $uid")
|
|
||||||
|
|
||||||
// Start heartbeat
|
|
||||||
val heartbeatJob = songScope.launch {
|
|
||||||
while (true) {
|
|
||||||
try {
|
|
||||||
send(Frame.Ping(ByteArray(0)))
|
|
||||||
delay(30000) // 30 seconds
|
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.error("Heartbeat failed for user $uid", e)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(status[uid] == SongType.STREAM_OFF) {
|
if(status[uid] == SongType.STREAM_OFF) {
|
||||||
songScope.launch {
|
songScope.launch {
|
||||||
sendSerialized(
|
sendSerialized(SongResponse(
|
||||||
SongResponse(
|
|
||||||
SongType.STREAM_OFF.value,
|
SongType.STREAM_OFF.value,
|
||||||
uid,
|
uid,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
)
|
))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
@ -142,58 +121,45 @@ fun Routing.wsSongRoutes() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
is Frame.Ping -> send(Frame.Pong(frame.data))
|
is Frame.Ping -> send(Frame.Pong(frame.data))
|
||||||
else -> {}
|
else -> {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch(e: ClosedReceiveChannelException) {
|
} catch(e: ClosedReceiveChannelException) {
|
||||||
logger.error("WebSocket connection closed for user $uid: ${e.message}")
|
logger.error("Error in WebSocket: ${e.message}")
|
||||||
} catch (e: Exception) {
|
|
||||||
logger.error("Unexpected error in WebSocket for user $uid", e)
|
|
||||||
} finally {
|
} finally {
|
||||||
logger.info("Cleaning up WebSocket connection for user $uid")
|
|
||||||
removeSession(uid, this)
|
removeSession(uid, this)
|
||||||
ackMap[uid]?.remove(this)
|
ackMap[uid]?.remove(this)
|
||||||
heartbeatJob.cancel()
|
|
||||||
}
|
|
||||||
} catch(e: Exception) {
|
|
||||||
logger.error("Unexpected error in WebSocket for user $uid", e)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
dispatcher.subscribe(SongEvent::class) {
|
dispatcher.subscribe(SongEvent::class) {
|
||||||
logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.current?.name)
|
logger.debug("SongEvent: {} / {} {}", it.uid, it.type, it.current?.name)
|
||||||
songScope.launch {
|
songScope.launch {
|
||||||
broadcastMessage(
|
broadcastMessage(it.uid, SongResponse(
|
||||||
it.uid, SongResponse(
|
|
||||||
it.type.value,
|
it.type.value,
|
||||||
it.uid,
|
it.uid,
|
||||||
it.reqUid,
|
it.reqUid,
|
||||||
it.current?.toSerializable(),
|
it.current?.toSerializable(),
|
||||||
it.next?.toSerializable(),
|
it.next?.toSerializable(),
|
||||||
it.delUrl
|
it.delUrl
|
||||||
)
|
))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
dispatcher.subscribe(TimerEvent::class) {
|
dispatcher.subscribe(TimerEvent::class) {
|
||||||
if(it.type == TimerType.STREAM_OFF) {
|
if(it.type == TimerType.STREAM_OFF) {
|
||||||
songScope.launch {
|
songScope.launch {
|
||||||
broadcastMessage(
|
broadcastMessage(it.uid, SongResponse(
|
||||||
it.uid, SongResponse(
|
|
||||||
it.type.value,
|
it.type.value,
|
||||||
it.uid,
|
it.uid,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
)
|
))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Serializable
|
@Serializable
|
||||||
data class SerializableYoutubeVideo(
|
data class SerializableYoutubeVideo(
|
||||||
val url: String,
|
val url: String,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user