some improve MessageHandler.kt

This commit is contained in:
dalbodeule 2024-07-26 19:20:10 +09:00
parent ff90e5fe7f
commit e4b6f819ca
No known key found for this signature in database
GPG Key ID: EFA860D069C9FA65

View File

@ -119,72 +119,75 @@ class MessageHandler(
var result = chat.first var result = chat.first
var isFail = false var isFail = false
result = dailyCounterPattern.replace(result) { // Replace dailyCounterPattern
val name = it.groupValues[1] result = dailyCounterPattern.replace(result) { matchResult ->
val name = matchResult.groupValues[1]
val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user) val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user)
return@replace if(dailyCounter.second) if (dailyCounter.second) {
CounterService.updateDailyCounterValue(name, msg.userId, 1, user).first.toString() CounterService.updateDailyCounterValue(name, msg.userId, 1, user).first.toString()
else { } else {
isFail = true isFail = true
dailyCounter.first.toString() dailyCounter.first.toString()
} }
} }
if(isFail && chat.second != "") { // Handle fail case
if (isFail && chat.second.isNotEmpty()) {
result = chat.second result = chat.second
result = dailyCounterPattern.replace(result) { result = dailyCounterPattern.replace(result) { matchResult ->
val name = it.groupValues[1] val name = matchResult.groupValues[1]
val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user) val dailyCounter = CounterService.getDailyCounterValue(name, msg.userId, user)
dailyCounter.first.toString() dailyCounter.first.toString()
} }
} }
result = followPattern.replace(result) { // Replace followPattern
result = followPattern.replace(result) { matchResult ->
try { try {
val followingDate = getFollowDate(listener.chatId, msg.userId) val followingDate = getFollowDate(listener.chatId, msg.userId)
.content.streamingProperty.following?.followDate .content.streamingProperty.following?.followDate
return@replace when (followingDate) { val period = followingDate?.let {
null -> "0" val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
else -> { val pastDate = LocalDateTime.parse(it, formatter)
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") val today = LocalDateTime.now()
val pastDate = LocalDateTime.parse(followingDate, formatter) ChronoUnit.DAYS.between(pastDate, today)
val today = LocalDateTime.now() } ?: 0
val period = ChronoUnit.DAYS.between(pastDate, today)
"$period" period.toString()
}
}
} catch (e: Exception) { } catch (e: Exception) {
logger.error(e.message) logger.error(e.message)
"0" "0"
} }
} }
result = daysPattern.replace(result) { // Replace daysPattern
val (year, month, day) = it.destructured result = daysPattern.replace(result) { matchResult ->
val (year, month, day) = matchResult.destructured
val pastDate = LocalDateTime.of(year.toInt(), month.toInt(), day.toInt(), 0, 0, 0) val pastDate = LocalDateTime.of(year.toInt(), month.toInt(), day.toInt(), 0, 0, 0)
val today = LocalDateTime.now() val today = LocalDateTime.now()
val daysBetween = ChronoUnit.DAYS.between(pastDate, today) val daysBetween = ChronoUnit.DAYS.between(pastDate, today)
daysBetween.toString() daysBetween.toString()
} }
result = counterPattern.replace(result) { // Replace counterPattern
val name = it.groupValues[1] result = counterPattern.replace(result) { matchResult ->
val name = matchResult.groupValues[1]
CounterService.updateCounterValue(name, 1, user).toString() CounterService.updateCounterValue(name, 1, user).toString()
} }
result = personalCounterPattern.replace(result) { // Replace personalCounterPattern
val name = it.groupValues[1] result = personalCounterPattern.replace(result) { matchResult ->
val name = matchResult.groupValues[1]
CounterService.updatePersonalCounterValue(name, msg.userId, 1, user).toString() CounterService.updatePersonalCounterValue(name, msg.userId, 1, user).toString()
} }
// Replace namePattern
result = namePattern.replace(result, userName) result = namePattern.replace(result, userName)
return result return result
} }
} }