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
+46
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
+4 -7
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,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))