Skip to content

Commit 31fac3c

Browse files
committed
fix: correct cooldown
Closes #66
1 parent 5e52c2a commit 31fac3c

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ plugins {
1313
}
1414

1515
group = "io.github.samarium150"
16-
version = "5.0.0-beta.2"
16+
version = "5.0.0-beta.3"
1717

1818
repositories {
1919
mavenLocal()

src/main/kotlin/io/github/samarium150/mirai/plugin/MiraiConsoleLolicon.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import java.net.Proxy
4747
object MiraiConsoleLolicon : KotlinPlugin(
4848
JvmPluginDescription(
4949
id = "io.github.samarium150.mirai.plugin.mirai-console-lolicon",
50-
version = "5.0.0-beta.2",
50+
version = "5.0.0-beta.3",
5151
name = "Lolicon"
5252
) {
5353
author("Samarium150")

src/main/kotlin/io/github/samarium150/mirai/plugin/util/CooldownUtil.kt

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,21 @@ object CooldownUtil {
4949
return groupLockMap.getOrPut(id) { Mutex() }
5050
}
5151

52+
private fun getLock(subject: Contact?): Mutex? {
53+
return when (subject) {
54+
is User -> getUserLock(subject.id)
55+
is Group -> getGroupLock(subject.id)
56+
else -> null
57+
}
58+
}
59+
60+
private fun removeLock(subject: Contact?) {
61+
when (subject) {
62+
is User -> userLockMap.remove(subject.id)
63+
is Group -> groupLockMap.remove(subject.id)
64+
}
65+
}
66+
5267
/**
5368
* 获取冷却状态
5469
*
@@ -57,11 +72,7 @@ object CooldownUtil {
5772
* @see CommandSender.subject
5873
*/
5974
fun getCooldownStatus(subject: Contact?): Boolean {
60-
return when(subject) {
61-
is User -> getUserLock(subject.id).isLocked
62-
is Group -> getGroupLock(subject.id).isLocked
63-
else -> false
64-
}
75+
return getLock(subject)?.isLocked ?: false
6576
}
6677

6778
/**
@@ -73,15 +84,11 @@ object CooldownUtil {
7384
*/
7485
@OptIn(DelicateCoroutinesApi::class)
7586
suspend fun cooldown(subject: Contact?, time: Int) = GlobalScope.launch {
76-
val mutex = when (subject) {
77-
is User -> userLockMap.remove(subject.id)
78-
is Group -> groupLockMap.remove(subject.id)
79-
else -> null
80-
}
81-
mutex?.withLock {
87+
getLock(subject)?.withLock {
8288
logger.info("${subject}进入冷却")
8389
delay(time * 1000L)
8490
logger.info("${subject}已冷却")
8591
}
92+
removeLock(subject)
8693
}
8794
}

0 commit comments

Comments
 (0)