Skip to content

Commit

Permalink
Add Anti Nudge Spammer
Browse files Browse the repository at this point in the history
  • Loading branch information
guimc233 committed Nov 18, 2022
1 parent d6527f3 commit 9b8e010
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 23 deletions.
26 changes: 3 additions & 23 deletions src/main/kotlin/ltd/guimc/lgzbot/PluginMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ltd.guimc.lgzbot.files.Config
import ltd.guimc.lgzbot.files.GithubSubConfig
import ltd.guimc.lgzbot.listener.message.GithubUrlListener
import ltd.guimc.lgzbot.listener.message.MessageFilter
import ltd.guimc.lgzbot.utils.MessageUtils.getPlainText
import ltd.guimc.lgzbot.listener.nudge.AntiNudgeSpam
import ltd.guimc.lgzbot.utils.RegexUtils.getDefaultPinyinRegex
import ltd.guimc.lgzbot.utils.RegexUtils.getDefaultRegex
import net.mamoe.mirai.console.command.BuiltInCommands
Expand All @@ -24,31 +24,25 @@ import net.mamoe.mirai.console.permission.AbstractPermitteeId
import net.mamoe.mirai.console.permission.Permission
import net.mamoe.mirai.console.permission.PermissionId
import net.mamoe.mirai.console.permission.PermissionService
import net.mamoe.mirai.console.permission.PermissionService.Companion.hasPermission
import net.mamoe.mirai.console.permission.PermitteeId.Companion.permitteeId
import net.mamoe.mirai.console.plugin.author
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescription
import net.mamoe.mirai.console.plugin.jvm.KotlinPlugin
import net.mamoe.mirai.console.plugin.name
import net.mamoe.mirai.console.plugin.version
import net.mamoe.mirai.contact.Friend
import net.mamoe.mirai.contact.Group
import net.mamoe.mirai.contact.getMember
import net.mamoe.mirai.event.EventPriority
import net.mamoe.mirai.event.GlobalEventChannel
import net.mamoe.mirai.event.events.*
import net.mamoe.mirai.message.data.ForwardMessage
import net.mamoe.mirai.message.data.ForwardMessageBuilder
import net.mamoe.mirai.message.data.PlainText
import net.mamoe.mirai.message.data.toMessageChain

object PluginMain : KotlinPlugin(
JvmPluginDescription(
"ltd.guimc.lgzbot.plugin",
"0.2.1",
"LgzBot",
){
author("汐洛 & YounKoo & 笨蛋们")
author("BakaBotTeam成员 & Everyone")
}
) {
lateinit var bypassMute: Permission
Expand All @@ -58,8 +52,6 @@ object PluginMain : KotlinPlugin(
lateinit var adPinyinRegex: Array<Regex>
var helpMessages: Array<ForwardMessage>? = null

val iI1I1i1I1i1: Regex = Regex("with <.*>")

override fun onEnable() {
logger.info("$name v$version by $author Loading")
adRegex = getDefaultRegex()
Expand Down Expand Up @@ -140,18 +132,6 @@ object PluginMain : KotlinPlugin(
it.bot.getFriendOrFail(it.fromId).sendMessage(PlainText("你好呀 大笨蛋!"))
}

subscribeAlways<MessagePreSendEvent> { e ->
if (e.target is Friend) if (iI1I1i1I1i1.containsMatchIn(
e.message.toMessageChain().getPlainText()
)
) e.intercept()
}

subscribeAlways<NudgeEvent> { e ->
if (e.target == e.bot && e.subject is Group && (e.subject as Group).permitteeId.hasPermission(
nudgeMute
)
) (e.subject as Group).getMember(e.from.id)!!.mute(600)
}
subscribeAlways<NudgeEvent>(priority = EventPriority.HIGHEST) { e -> AntiNudgeSpam.onNudge(e) }
}
}
50 changes: 50 additions & 0 deletions src/main/kotlin/ltd/guimc/lgzbot/listener/nudge/AntiNudgeSpam.kt
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) {
}
}
}
}

0 comments on commit 9b8e010

Please sign in to comment.