feat: 完善黑名单机制 (#191)

This commit is contained in:
Rock Chin 2023-02-27 05:57:45 +00:00
parent e161343d72
commit 0c3d911e74
4 changed files with 66 additions and 14 deletions

2
.gitignore vendored
View File

@ -4,7 +4,7 @@ __pycache__/
database.db
qchatgpt.log
config.py
banlist.py
/banlist.py
plugins/
!plugins/__init__.py
/revcfg.py

View File

@ -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] # 本项目交流群的群号,默认屏蔽,避免在交流群测试机器人

46
pkg/qqbot/banlist.py Normal file
View File

@ -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

View File

@ -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,12 +49,8 @@ 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))
if banlist.is_banned(launcher_type, launcher_id):
logging.info("根据禁用列表忽略{}_{}的消息".format(launcher_type, launcher_id))
return []
if ignore.ignore(text_message):