diff --git a/src/infrastructure/config/config_manager.py b/src/infrastructure/config/config_manager.py index 2afc39b..bb22124 100644 --- a/src/infrastructure/config/config_manager.py +++ b/src/infrastructure/config/config_manager.py @@ -61,44 +61,56 @@ class ConfigManager: glist = [str(g) for g in self.get_group_list()] target = str(group_id_or_umo) + # 核心 ID 解析 target_simple_id = target.split(":")[-1] if ":" in target else target - target_parent_id = ( + + # 1. 剥离话题 ID (#) + target_without_topic = ( target_simple_id.split("#", 1)[0] if "#" in target_simple_id else target_simple_id ) - def _is_match( - item: str, - target: str, - target_simple_id: str, - target_parent_id: str, - ) -> bool: + # 2. 剥离独立会话的前置用户 ID (_) + # 例如: 903928770_1007096543 -> 1007096543 + target_actual_group_id = ( + target_without_topic.split("_", 1)[-1] + if "_" in target_without_topic + else target_without_topic + ) + + def _is_match(item: str) -> bool: + # 1. 完整/前缀 UMO 匹配 (包含平台前缀) if ":" in item: if item == target: return True - # 允许 Telegram 话题会话通过“父 UMO”命中, - # 例如: item=telegram2:GroupMessage:-1001 - # target=telegram2:GroupMessage:-1001#2264 - if "#" in target_simple_id: - if ":" not in target: - return False - item_prefix, item_tail = item.rsplit(":", 1) - target_prefix, _ = target.rsplit(":", 1) - return ( - item_prefix == target_prefix and item_tail == target_parent_id - ) - return False - if item == target_simple_id: - return True - # 允许 Telegram 话题会话通过父群 ID 命中简单群号白/黑名单 - return "#" in target_simple_id and item == target_parent_id + # 若 target 有平台前缀,尝试前缀匹配 + if ":" in target: + try: + item_prefix, item_tail = item.rsplit(":", 1) + target_prefix, _ = target.rsplit(":", 1) - is_in_list = any( - _is_match(item, target, target_simple_id, target_parent_id) - for item in glist - ) + if item_prefix == target_prefix: + # 命中了清理后的群 ID (支持剥离话题或剥离独立会话前级) + if item_tail in ( + target_without_topic, + target_actual_group_id, + ): + return True + except (ValueError, IndexError): + pass + return False + + # 2. 简单 ID 匹配 (无平台前缀) + # 命中了原始 ID、剥话题后的 ID 或剥离独立会话前缀后的 ID + return item in ( + target_simple_id, + target_without_topic, + target_actual_group_id, + ) + + is_in_list = any(_is_match(item) for item in glist) if mode == "whitelist": return is_in_list diff --git a/src/infrastructure/platform/adapters/onebot_adapter.py b/src/infrastructure/platform/adapters/onebot_adapter.py index f6f4020..b946e67 100644 --- a/src/infrastructure/platform/adapters/onebot_adapter.py +++ b/src/infrastructure/platform/adapters/onebot_adapter.py @@ -69,6 +69,18 @@ class OneBotAdapter(PlatformAdapter): """从支持的尺寸列表中找到最接近请求尺寸的一个。""" return min(self.AVAILABLE_SIZES, key=lambda x: abs(x - requested_size)) + def _parse_id(self, id_str: str) -> int: + """ + 解析复合 ID (如 1234567890(用户ID)_123456789(群ID)) 并返回真实的整数 ID。 + 支持剥离下划线 (_) 分隔的前缀。 + """ + try: + # 必须先 split,因为 int("1_2") 在 Python 里会变成 12 + actual_id = str(id_str).split("_")[-1] + return int(actual_id) + except (ValueError, IndexError): + return 0 + # ==================== IMessageRepository 实现 ==================== async def fetch_messages( @@ -97,7 +109,7 @@ class OneBotAdapter(PlatformAdapter): # 调用 OneBot 标准 API: get_group_msg_history result = await self.bot.call_action( "get_group_msg_history", - group_id=int(group_id), + group_id=self._parse_id(group_id), count=max_count, ) @@ -341,7 +353,7 @@ class OneBotAdapter(PlatformAdapter): await self.bot.call_action( "send_group_msg", - group_id=int(group_id), + group_id=self._parse_id(group_id), message=message, ) return True @@ -383,7 +395,7 @@ class OneBotAdapter(PlatformAdapter): await self.bot.call_action( "send_group_msg", - group_id=int(group_id), + group_id=self._parse_id(group_id), message=message, ) return True @@ -411,7 +423,7 @@ class OneBotAdapter(PlatformAdapter): try: await self.bot.call_action( "upload_group_file", - group_id=int(group_id), + group_id=self._parse_id(group_id), file=file_path, name=filename or file_path.replace("\\", "/").split("/")[-1], ) @@ -447,7 +459,7 @@ class OneBotAdapter(PlatformAdapter): await self.bot.call_action( "send_group_forward_msg", - group_id=int(group_id), + group_id=self._parse_id(group_id), messages=nodes, ) return True @@ -462,7 +474,7 @@ class OneBotAdapter(PlatformAdapter): try: result = await self.bot.call_action( "get_group_info", - group_id=int(group_id), + group_id=self._parse_id(group_id), ) if not result: @@ -492,7 +504,7 @@ class OneBotAdapter(PlatformAdapter): try: result = await self.bot.call_action( "get_group_member_list", - group_id=int(group_id), + group_id=self._parse_id(group_id), ) members = [] @@ -519,8 +531,8 @@ class OneBotAdapter(PlatformAdapter): try: result = await self.bot.call_action( "get_group_member_info", - group_id=int(group_id), - user_id=int(user_id), + group_id=self._parse_id(group_id), + user_id=self._parse_id(user_id), ) if not result: