From 1b611ecce5178d71963403b8b1032d1bfcdc161f Mon Sep 17 00:00:00 2001 From: ChangingSelf Date: Mon, 10 Mar 2025 22:52:14 +0800 Subject: [PATCH] =?UTF-8?q?resolve=20SengokuCola/MaiMBot#167=20=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E6=AD=A3=E5=88=99=E8=A1=A8=E8=BE=BE=E5=BC=8F=E8=BF=87?= =?UTF-8?q?=E6=BB=A4=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugins/chat/bot.py | 9 +++++++++ src/plugins/chat/config.py | 4 ++++ template/bot_config_template.toml | 8 +++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/plugins/chat/bot.py b/src/plugins/chat/bot.py index c510fe4bf..c72ff2380 100644 --- a/src/plugins/chat/bot.py +++ b/src/plugins/chat/bot.py @@ -1,3 +1,4 @@ +import re import time from random import random @@ -75,6 +76,14 @@ class ChatBot: logger.info(f"[过滤词识别]消息中含有{word},filtered") return + # 正则表达式过滤 + for pattern in global_config.ban_words_regex: + if re.search(pattern, message.detailed_plain_text): + logger.info( + f"[{message.group_name}]{message.user_nickname}:{message.processed_plain_text}") + logger.info(f"[正则表达式过滤]消息匹配到{pattern},filtered") + return + current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(message.time)) # topic=await topic_identifier.identify_topic_llm(message.processed_plain_text) diff --git a/src/plugins/chat/config.py b/src/plugins/chat/config.py index c37d23a46..7a7e4ebc9 100644 --- a/src/plugins/chat/config.py +++ b/src/plugins/chat/config.py @@ -43,6 +43,7 @@ class BotConfig: EMOJI_CHECK_PROMPT: str = "符合公序良俗" # 表情包过滤要求 ban_words = set() + ban_words_regex = set() max_response_length: int = 1024 # 最大回复长度 @@ -277,6 +278,9 @@ class BotConfig: config.response_interested_rate_amplifier = msg_config.get("response_interested_rate_amplifier", config.response_interested_rate_amplifier) config.down_frequency_rate = msg_config.get("down_frequency_rate", config.down_frequency_rate) + + if config.INNER_VERSION in SpecifierSet(">=0.0.5"): + config.ban_words_regex = msg_config.get("ban_words_regex", config.ban_words_regex) def memory(parent: dict): memory_config = parent["memory"] diff --git a/template/bot_config_template.toml b/template/bot_config_template.toml index bff64d05f..44c80fd2f 100644 --- a/template/bot_config_template.toml +++ b/template/bot_config_template.toml @@ -1,5 +1,5 @@ [inner] -version = "0.0.4" +version = "0.0.5" #如果你想要修改配置文件,请在修改后将version的值进行变更 #如果新增项目,请在BotConfig类下新增相应的变量 @@ -40,6 +40,12 @@ ban_words = [ # "403","张三" ] +ban_words_regex = [ + # 需要过滤的消息匹配的正则表达式,匹配到的消息将被过滤,若不了解正则表达式请勿修改 + #"https?://[^\\s]+", # 匹配https链接 + #"\\d{4}-\\d{2}-\\d{2}", # 匹配日期 +] + [emoji] check_interval = 120 # 检查表情包的时间间隔 register_interval = 10 # 注册表情包的时间间隔