Skip to content

Commit

Permalink
fix: correct cooldown
Browse files Browse the repository at this point in the history
Closes #66
  • Loading branch information
Samarium150 committed Dec 18, 2021
1 parent 5e52c2a commit 31fac3c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

group = "io.github.samarium150"
version = "5.0.0-beta.2"
version = "5.0.0-beta.3"

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import java.net.Proxy
object MiraiConsoleLolicon : KotlinPlugin(
JvmPluginDescription(
id = "io.github.samarium150.mirai.plugin.mirai-console-lolicon",
version = "5.0.0-beta.2",
version = "5.0.0-beta.3",
name = "Lolicon"
) {
author("Samarium150")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ object CooldownUtil {
return groupLockMap.getOrPut(id) { Mutex() }
}

private fun getLock(subject: Contact?): Mutex? {
return when (subject) {
is User -> getUserLock(subject.id)
is Group -> getGroupLock(subject.id)
else -> null
}
}

private fun removeLock(subject: Contact?) {
when (subject) {
is User -> userLockMap.remove(subject.id)
is Group -> groupLockMap.remove(subject.id)
}
}

/**
* 获取冷却状态
*
Expand All @@ -57,11 +72,7 @@ object CooldownUtil {
* @see CommandSender.subject
*/
fun getCooldownStatus(subject: Contact?): Boolean {
return when(subject) {
is User -> getUserLock(subject.id).isLocked
is Group -> getGroupLock(subject.id).isLocked
else -> false
}
return getLock(subject)?.isLocked ?: false
}

/**
Expand All @@ -73,15 +84,11 @@ object CooldownUtil {
*/
@OptIn(DelicateCoroutinesApi::class)
suspend fun cooldown(subject: Contact?, time: Int) = GlobalScope.launch {
val mutex = when (subject) {
is User -> userLockMap.remove(subject.id)
is Group -> groupLockMap.remove(subject.id)
else -> null
}
mutex?.withLock {
getLock(subject)?.withLock {
logger.info("${subject}进入冷却")
delay(time * 1000L)
logger.info("${subject}已冷却")
}
removeLock(subject)
}
}

0 comments on commit 31fac3c

Please sign in to comment.