diff --git a/build.gradle.kts b/build.gradle.kts index c2c12bd..dfe39cb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -81,8 +81,8 @@ dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.9.0-RC") // https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-reflect implementation("org.jetbrains.kotlin:kotlin-reflect:2.0.0") - // https://mvnrepository.com/artifact/org.reflections/reflections - implementation("org.reflections:reflections:0.10.2") + + implementation("com.google.code.gson:gson:2.11.0") // https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client implementation("org.mariadb.jdbc:mariadb-java-client:3.4.0") diff --git a/src/main/kotlin/space/mori/chzzk_bot/chzzk/MessageHandler.kt b/src/main/kotlin/space/mori/chzzk_bot/chzzk/MessageHandler.kt index fe17cc1..76475f3 100644 --- a/src/main/kotlin/space/mori/chzzk_bot/chzzk/MessageHandler.kt +++ b/src/main/kotlin/space/mori/chzzk_bot/chzzk/MessageHandler.kt @@ -8,6 +8,11 @@ import space.mori.chzzk_bot.services.UserService import xyz.r2turntrue.chzzk4j.chat.ChatMessage import xyz.r2turntrue.chzzk4j.chat.ChzzkChat import xyz.r2turntrue.chzzk4j.types.channel.ChzzkChannel +import java.time.LocalDate +import java.time.Period +import java.time.format.DateTimeFormatter + + class MessageHandler( private val channel: ChzzkChannel, @@ -20,6 +25,7 @@ class MessageHandler( private val personalCounterPattern = Regex("]+)>") private val dailyCounterPattern = Regex("]+)>") private val namePattern = Regex("") + private val followPattern = Regex("") init { reloadCommand() @@ -34,7 +40,7 @@ class MessageHandler( this.commands.put(it.command.lowercase()) { msg, user -> logger.debug("${channel.channelName} - ${it.command} - ${it.content}/${it.failContent}") - val result = replaceCounters(Pair(it.content, it.failContent), user, msg.userId, msg.profile?.nickname ?: "") + val result = replaceCounters(Pair(it.content, it.failContent), user, msg, listener, msg.profile?.nickname ?: "") listener.sendChat(result) } } @@ -46,7 +52,7 @@ class MessageHandler( commands[commandKey.lowercase()]?.let { it(msg, user) } } - private fun replaceCounters(chat: Pair, user: User, userId: String, userName: String): String { + private fun replaceCounters(chat: Pair, user: User, msg: ChatMessage, listener: ChzzkChat, userName: String): String { var result = chat.first var isFail = false @@ -57,31 +63,49 @@ class MessageHandler( result = personalCounterPattern.replace(result) { val name = it.groupValues[1] - CounterService.updatePersonalCounterValue(name, userId, 1, user).toString() + CounterService.updatePersonalCounterValue(name, msg.userId, 1, user).toString() } result = dailyCounterPattern.replace(result) { val name = it.groupValues[1] - val dailyCounter = CounterService.getDailyCounterValue(name, userId, user) + val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user) return@replace if(dailyCounter.second) - CounterService.updateDailyCounterValue(name, userId, 1, user).first.toString() + CounterService.updateDailyCounterValue(name, msg.userId, 1, user).first.toString() else { isFail = true dailyCounter.first.toString() } } - if(isFail) { + if(isFail && chat.second != "") { result = chat.second result = dailyCounterPattern.replace(result) { val name = it.groupValues[1] - val dailyCounter = CounterService.getDailyCounterValue(name, userId, user) + val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user) dailyCounter.first.toString() } } + result = followPattern.replace(result) { + val following = getFollowDate("", msg.userId) + val dateString: String? = following.content.streamingProperty["followDate"] + val today = LocalDate.now() + + if(dateString == null) { + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") + // 문자열을 LocalDate 객체로 변환 + val pastDate = LocalDate.parse(dateString, formatter) + + val period = Period.between(pastDate, today) + period.days.toString() + } else "" + } + if(isFail) { + return chat.second + } + result = namePattern.replace(result, userName) return result diff --git a/src/main/kotlin/space/mori/chzzk_bot/chzzk/getFollowDate.kt b/src/main/kotlin/space/mori/chzzk_bot/chzzk/getFollowDate.kt new file mode 100644 index 0000000..bdc4959 --- /dev/null +++ b/src/main/kotlin/space/mori/chzzk_bot/chzzk/getFollowDate.kt @@ -0,0 +1,48 @@ +package space.mori.chzzk_bot.chzzk + +import com.google.gson.Gson +import okhttp3.OkHttpClient +import okhttp3.Request +import java.io.IOException + +data class IFollow( + val code: Int = 200, + val message: String? = null, + val content: IFollowContent = IFollowContent() +) + +data class IFollowContent( + val userIdHash: String = "", + val nickname: String = "", + val profileImageUrl: String = "", + val userRoleCode: String = "", + val badge: String? = null, + val title: String? = null, + val verifiedMark: Boolean = false, + val activityBadges: List = emptyList(), + val streamingProperty: Map = mapOf( + "following" to "", + "nicknameColor" to "" + ) +) + +val client = OkHttpClient() +val gson = Gson() + +fun getFollowDate(chatID: String, userId: String) : IFollow { + val url = "https://comm-api.game.naver.com/nng_main/v1/chats/$chatID/users/$userId/profile-card?chatType=STREAMING" + val request = Request.Builder() + .url(url) + .build() + + client.newCall(request).execute().use { response -> + try { + if(!response.isSuccessful) throw IOException("Unexpected code ${response.code}") + val follow = gson.fromJson(response.body?.string(), IFollow::class.java) + + return follow + } catch(e: Exception) { + throw e + } + } +} \ No newline at end of file