diff --git a/src/config.py b/src/config.py index 806fe21..8ac6c3d 100644 --- a/src/config.py +++ b/src/config.py @@ -58,6 +58,7 @@ class Config: self.list_type: str = raw_config["Chat"].get("list_type") self.group_list: list = raw_config["Chat"].get("group_list", []) self.user_list: list = raw_config["Chat"].get("user_list", []) + self.ban_user_id: list = raw_config["Chat"].get("ban_user_id", []) if not self.list_type or self.list_type not in ["whitelist", "blacklist"]: logger.critical("请在配置文件中指定list_type或list_type填写错误") sys.exit(1) diff --git a/src/recv_handler.py b/src/recv_handler.py index 6034366..71df21c 100644 --- a/src/recv_handler.py +++ b/src/recv_handler.py @@ -5,7 +5,7 @@ import time import asyncio import json import websockets as Server -from typing import List, Tuple +from typing import List, Tuple, Optional import uuid from . import MetaEventType, RealMessageType, MessageType, NoticeType @@ -65,6 +65,35 @@ class RecvHandler: logger.debug("心跳正常") await asyncio.sleep(self.interval) + def check_allow_to_chat(self, user_id: int, group_id: Optional[int]) -> bool: + # sourcery skip: hoist-statement-from-if, merge-else-if-into-elif + """ + 检查是否允许聊天 + Parameters: + user_id: int: 用户ID + group_id: int: 群ID + Returns: + bool: 是否允许聊天 + """ + if group_id: + if global_config.list_type == "whitelist" and group_id not in global_config.group_list: + logger.warning("群聊不在白名单中,消息被丢弃") + return False + elif global_config.list_type == "blacklist" and group_id in global_config.group_list: + logger.warning("群聊在黑名单中,消息被丢弃") + return False + else: + if global_config.list_type == "whitelist" and user_id not in global_config.user_list: + logger.warning("用户不在白名单中,消息被丢弃") + return False + elif global_config.list_type == "blacklist" and user_id in global_config.user_list: + logger.warning("用户在黑名单中,消息被丢弃") + return False + if user_id in global_config.ban_user_id: + logger.warning("用户在黑名单中,消息被丢弃") + return False + return True + async def handle_raw_message(self, raw_message: dict) -> None: # sourcery skip: low-code-quality, remove-unreachable-code """ @@ -88,11 +117,7 @@ class RecvHandler: if sub_type == MessageType.Private.friend: sender_info: dict = raw_message.get("sender") - if global_config.list_type == "whitelist" and sender_info.get("user_id") not in global_config.user_list: - logger.warning("用户不在白名单中,消息被丢弃") - return None - if global_config.list_type == "blacklist" and sender_info.get("user_id") in global_config.user_list: - logger.warning("用户在黑名单中,消息被丢弃") + if not self.check_allow_to_chat(sender_info.get("user_id"), None): return None # 发送者用户信息 @@ -151,14 +176,7 @@ class RecvHandler: if sub_type == MessageType.Group.normal: sender_info: dict = raw_message.get("sender") - if ( - global_config.list_type == "whitelist" - and raw_message.get("group_id") not in global_config.group_list - ): - logger.warning("群聊不在白名单中,消息被丢弃") - return None - if global_config.list_type == "blacklist" and raw_message.get("group_id") in global_config.group_list: - logger.warning("群聊在黑名单中,消息被丢弃") + if not self.check_allow_to_chat(sender_info.get("user_id"), raw_message.get("group_id")): return None # 发送者用户信息 diff --git a/template/template_config.toml b/template/template_config.toml index e552646..dba4b5d 100644 --- a/template/template_config.toml +++ b/template/template_config.toml @@ -17,6 +17,7 @@ list_type = "whitelist" # 使用的白名单类型,可选为:whitelist, blac # 当list_type为blacklist时,使用黑名单模式,以下两个列表的含义是:禁止名单中的人聊天 group_list = [] # 群组名单 private_list = [] # 私聊名单 +ban_user_id = [] # 禁止名单(禁止名单中的人无法使用任何功能) [Voice] # 发送语音设置 use_tts = false # 是否使用tts语音(请确保你配置了tts并有对应的adapter)