follow period message handler add.

but not yet activated...
This commit is contained in:
dalbodeule
2024-06-14 16:03:09 +09:00
parent e85561dd74
commit 557600f812
3 changed files with 81 additions and 9 deletions

View File

@@ -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")

View File

@@ -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("<counter_personal:([^>]+)>")
private val dailyCounterPattern = Regex("<daily_counter:([^>]+)>")
private val namePattern = Regex("<name>")
private val followPattern = Regex("<following>")
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<String, String>, user: User, userId: String, userName: String): String {
private fun replaceCounters(chat: Pair<String, String>, 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

View File

@@ -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<String> = emptyList(),
val streamingProperty: Map<String, String> = 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
}
}
}