diff --git a/config-template.py b/config-template.py index b0d9592a..dc3b2506 100644 --- a/config-template.py +++ b/config-template.py @@ -83,6 +83,18 @@ response_rules = { "regexp": [] # "为什么.*", "怎么?样.*", "怎么.*", "如何.*", "[Hh]ow to.*", "[Ww]hy not.*", "[Ww]hat is.*", ".*怎么办", ".*咋办" } +# 消息忽略规则 +# 适用于私聊及群聊 +# 符合此规则的消息将不会被响应 +# 支持消息前缀匹配及正则表达式匹配 +# 此设置优先级高于response_rules +# 用以过滤mirai等其他层级的指令 +# @see https://github.com/RockChinQ/QChatGPT/issues/165 +ignore_rules = { + "prefix": ["/"], + "regexp": [] +} + # 敏感词过滤开关,以同样数量的*代替敏感词回复 # 请在sensitive.json中添加敏感词 sensitive_word_filter = True diff --git a/pkg/qqbot/ignore.py b/pkg/qqbot/ignore.py new file mode 100644 index 00000000..01994b2e --- /dev/null +++ b/pkg/qqbot/ignore.py @@ -0,0 +1,19 @@ +import re + + +def ignore(msg: str) -> bool: + """检查消息是否应该被忽略""" + import config + + if not hasattr(config, 'ignore_rules'): + return False + + if 'prefix' in config.ignore_rules: + for rule in config.ignore_rules['prefix']: + if msg.startswith(rule): + return True + + if 'regexp' in config.ignore_rules: + for rule in config.ignore_rules['regexp']: + if re.search(rule, msg): + return True diff --git a/pkg/qqbot/process.py b/pkg/qqbot/process.py index cd4e3e58..f81c11a0 100644 --- a/pkg/qqbot/process.py +++ b/pkg/qqbot/process.py @@ -22,6 +22,7 @@ import pkg.qqbot.command import pkg.plugin.host as plugin_host import pkg.plugin.models as plugin_models +import pkg.qqbot.ignore as ignore processing = [] @@ -44,6 +45,10 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes logging.info("根据禁用列表忽略群{}的消息".format(launcher_id)) return [] + if ignore.ignore(text_message): + logging.info("根据忽略规则忽略消息: {}".format(text_message)) + return [] + # 检查是否被禁言 if launcher_type == 'group': result = mgr.bot.member_info(target=launcher_id, member_id=mgr.bot.qq).get()