generated from project-mirai/mirai-console-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/kotlin/ltd/guimc/lgzbot/listener/nudge/AntiNudgeSpam.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ltd.guimc.lgzbot.listener.nudge | ||
|
||
import net.mamoe.mirai.contact.User | ||
import net.mamoe.mirai.event.events.NudgeEvent | ||
import net.mamoe.mirai.message.data.At | ||
import net.mamoe.mirai.message.data.PlainText | ||
import java.time.Instant | ||
|
||
object AntiNudgeSpam { | ||
private val nudgeTimes: MutableMap<User, Int> = mutableMapOf() | ||
private val lastNudgeTime: MutableMap<User, Long> = mutableMapOf() | ||
private val blockedUser: MutableMap<User, Long> = mutableMapOf() | ||
|
||
suspend fun onNudge(e: NudgeEvent) { | ||
val from = e.from as User | ||
val timestamp = Instant.now().epochSecond | ||
|
||
if (e.target != e.bot) return // 只处理对机器人的戳一戳 | ||
if (e.from == e.bot) return // 机器人自己戳自己? 雾 | ||
|
||
// 检查是否已经被屏蔽了 | ||
if (blockedUser[from] != null && blockedUser[from]!! >= timestamp) { | ||
e.intercept() | ||
return | ||
} | ||
|
||
if (nudgeTimes[from] == null) { // 检查是否已初始化过戳一戳发起者 | ||
nudgeTimes[from] = 1 | ||
lastNudgeTime[from] = timestamp | ||
} else if (timestamp - lastNudgeTime[from]!! >= 3) { | ||
// ta已经好久没有戳一戳了 重置次数 | ||
nudgeTimes[from] = 1 | ||
lastNudgeTime[from] = timestamp | ||
} else { | ||
// 次数+1 | ||
nudgeTimes[from] = nudgeTimes[from]!! + 1 | ||
} | ||
|
||
// 检查次数 | ||
if (nudgeTimes[from]!! >= 3) { | ||
blockedUser[from] = timestamp + 3600L // 3600s == 1h! | ||
try { | ||
e.subject.sendMessage( | ||
At(from) + PlainText(" 你...你怎么能戳这么快!\n我生气了!免疫你的戳一戳1小时!") | ||
) | ||
} catch (ignore: Throwable) { | ||
} | ||
} | ||
} | ||
} |