From 0c3d911e747b3847658784050ad75743f7732405 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Mon, 27 Feb 2023 05:57:45 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=E9=BB=91=E5=90=8D?= =?UTF-8?q?=E5=8D=95=E6=9C=BA=E5=88=B6=20(#191)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- banlist-template.py | 21 ++++++++++++++------ pkg/qqbot/banlist.py | 46 ++++++++++++++++++++++++++++++++++++++++++++ pkg/qqbot/process.py | 11 ++++------- 4 files changed, 66 insertions(+), 14 deletions(-) create mode 100644 pkg/qqbot/banlist.py diff --git a/.gitignore b/.gitignore index f78cf75..d93fc56 100644 --- a/.gitignore +++ b/.gitignore @@ -4,7 +4,7 @@ __pycache__/ database.db qchatgpt.log config.py -banlist.py +/banlist.py plugins/ !plugins/__init__.py /revcfg.py diff --git a/banlist-template.py b/banlist-template.py index 27f9aed..4b4d240 100644 --- a/banlist-template.py +++ b/banlist-template.py @@ -1,11 +1,20 @@ -# 禁用列表 +# 是否启用禁用列表 +enable = True + +# 禁用规则(黑名单) # person为个人,其中的QQ号会被禁止与机器人进行私聊或群聊交互 # 示例: person = [2854196310, 1234567890, 9876543210] # group为群组,其中的群号会被禁止与机器人进行交互 # 示例: group = [123456789, 987654321, 1234567890] - -# 是否启用禁用列表 -enable = True - +# +# 支持正则表达式,字符串都将被识别为正则表达式,例如: +# person = [12345678, 87654321, "2854.*"] +# group = [123456789, 987654321, "1234.*"] +# 若要排除某个QQ号或群号(即允许使用),可以在前面加上"!",例如: +# person = ["!1234567890"] +# group = ["!987654321"] +# 排除规则优先级高于包含规则,即如果同时存在包含规则和排除规则,排除规则将生效,例如: +# person = ["1234.*", "!1234567890"] +# 那么1234567890将不会被禁用,而其他以1234开头的QQ号都会被禁用 person = [2854196310] # 2854196310是Q群管家机器人的QQ号,默认屏蔽以免出现循环 -group = [204785790] # 204785790是本项目交流群的群号,默认屏蔽,避免在交流群测试机器人 +group = [204785790, 691226829] # 本项目交流群的群号,默认屏蔽,避免在交流群测试机器人 diff --git a/pkg/qqbot/banlist.py b/pkg/qqbot/banlist.py new file mode 100644 index 0000000..0296c85 --- /dev/null +++ b/pkg/qqbot/banlist.py @@ -0,0 +1,46 @@ +import pkg.utils.context + + +def is_banned(launcher_type: str, launcher_id: int) -> bool: + if not pkg.utils.context.get_qqbot_manager().enable_banlist: + return False + + result = False + + if launcher_type == 'group': + for group_rule in pkg.utils.context.get_qqbot_manager().ban_group: + if type(group_rule) == int: + if group_rule == launcher_id: # 此群群号被禁用 + result = True + elif type(group_rule) == str: + if group_rule.startswith('!'): + # 截取!后面的字符串作为表达式,判断是否匹配 + reg_str = group_rule[1:] + import re + if re.match(reg_str, str(launcher_id)): # 被豁免,最高级别 + result = False + break + else: + # 判断是否匹配regexp + import re + if re.match(group_rule, str(launcher_id)): # 此群群号被禁用 + result = True + + else: + # ban_person, 与群规则相同 + for person_rule in pkg.utils.context.get_qqbot_manager().ban_person: + if type(person_rule) == int: + if person_rule == launcher_id: + result = True + elif type(person_rule) == str: + if person_rule.startswith('!'): + reg_str = person_rule[1:] + import re + if re.match(reg_str, str(launcher_id)): + result = False + break + else: + import re + if re.match(person_rule, str(launcher_id)): + result = True + return result diff --git a/pkg/qqbot/process.py b/pkg/qqbot/process.py index 16b4685..c1e39af 100644 --- a/pkg/qqbot/process.py +++ b/pkg/qqbot/process.py @@ -25,6 +25,7 @@ import pkg.qqbot.ratelimit as ratelimit import pkg.plugin.host as plugin_host import pkg.plugin.models as plugin_models import pkg.qqbot.ignore as ignore +import pkg.qqbot.banlist as banlist processing = [] @@ -48,13 +49,9 @@ def process_message(launcher_type: str, launcher_id: int, text_message: str, mes session_name = "{}_{}".format(launcher_type, launcher_id) # 检查发送方是否被禁用 - if pkg.utils.context.get_qqbot_manager().enable_banlist: - if sender_id in pkg.utils.context.get_qqbot_manager().ban_person: - logging.info("根据禁用列表忽略用户{}的消息".format(sender_id)) - return [] - if launcher_type == 'group' and launcher_id in pkg.utils.context.get_qqbot_manager().ban_group: - logging.info("根据禁用列表忽略群{}的消息".format(launcher_id)) - return [] + if banlist.is_banned(launcher_type, launcher_id): + logging.info("根据禁用列表忽略{}_{}的消息".format(launcher_type, launcher_id)) + return [] if ignore.ignore(text_message): logging.info("根据忽略规则忽略消息: {}".format(text_message))