Skip to content
This repository has been archived by the owner on Nov 24, 2024. It is now read-only.

Commit

Permalink
fix(self-protect): failed replace
Browse files Browse the repository at this point in the history
  • Loading branch information
guimc233 committed Sep 30, 2024
1 parent f18fe6c commit f7d37f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ltd.guimc.lgzbot.listener.message

import ltd.guimc.lgzbot.PluginMain
import ltd.guimc.lgzbot.utils.MessageUtils.getPlainText
import ltd.guimc.lgzbot.utils.RegexUtils
import ltd.guimc.lgzbot.utils.RegexUtils.replaceRegex
import net.mamoe.mirai.event.EventHandler
Expand All @@ -14,18 +15,20 @@ import net.mamoe.mirai.message.data.toMessageChain
object SelfMessageListener : ListenerHost {
@EventHandler(priority = EventPriority.HIGHEST)
suspend fun MessagePreSendEvent.onEvent() {
val newMessageChain = MessageChainBuilder()
this.message.toMessageChain().forEach { it ->
if (it is PlainText && RegexUtils.matchRegex(PluginMain.spRegex, it.content)) {
this.cancel()
newMessageChain.add(it.content.replaceRegex(PluginMain.spRegex))
} else {
newMessageChain.add(it)
if (!this.message.toMessageChain().getPlainText().endsWith(" [Self-protect System]")) {
val newMessageChain = MessageChainBuilder()
this.message.toMessageChain().forEach { it ->
if (it is PlainText && RegexUtils.matchRegex(PluginMain.spRegex, it.content)) {
this.cancel()
newMessageChain.add(it.content.replaceRegex(PluginMain.spRegex))
} else {
newMessageChain.add(it)
}
}
if (this.isCancelled) {
newMessageChain.add(" [Self-protect System]")
this.target.sendMessage(newMessageChain.asMessageChain())
}
}
if (this.isCancelled) {
newMessageChain.add(" [Self-protect System]")
this.target.sendMessage(newMessageChain.asMessageChain())
}
}
}
2 changes: 1 addition & 1 deletion src/main/kotlin/ltd/guimc/lgzbot/utils/RegexUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ object RegexUtils {
i++
if (regex.containsMatchIn(a)) {
logger.info("匹配成功 在第${i}${regex.find(a)?.value}")
a.replace(regex, "***")
a = a.replace(regex, "***")
}
}
} catch (_: Throwable) {
Expand Down

0 comments on commit f7d37f7

Please sign in to comment.