From 0709b8f526f5bdab499a5c45b0c9bd82323cd7ab Mon Sep 17 00:00:00 2001 From: dalbodeule <11470513+dalbodeule@users.noreply.github.com> Date: Sat, 17 May 2025 14:37:39 +0900 Subject: [PATCH] [feature] text size limited 100, 100ms delay added. --- .../chzzk_bot/chatbot/chzzk/ChzzkHandler.kt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt index 2ac36a1..d61ac1a 100644 --- a/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt +++ b/chatbot/src/main/kotlin/space/mori/chzzk_bot/chatbot/chzzk/ChzzkHandler.kt @@ -1,7 +1,9 @@ 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 org.koin.java.KoinJavaComponent.inject @@ -25,6 +27,7 @@ import xyz.r2turntrue.chzzk4j.types.channel.live.ChzzkLiveStatus import java.lang.Exception import java.net.SocketTimeoutException import java.time.LocalDateTime +import java.nio.charset.Charset object ChzzkHandler { private val handlers = mutableListOf() @@ -325,7 +328,21 @@ class UserHandler( } } + private fun String.limitUtf8Length(maxBytes: Int): String { + val bytes = this.toByteArray(Charset.forName("UTF-8")) + if (bytes.size <= maxBytes) return this + var truncatedString = this + while (truncatedString.toByteArray(Charset.forName("UTF-8")).size > maxBytes) { + truncatedString = truncatedString.substring(0, truncatedString.length - 1) + } + return truncatedString + } + + @OptIn(DelicateCoroutinesApi::class) internal fun sendChat(msg: String) { - client.sendChatToLoggedInChannel(msg) + GlobalScope.launch { + delay(100L) + client.sendChatToLoggedInChannel(msg.limitUtf8Length(100)) + } } }