fix: 回退 umo 处理

This commit is contained in:
SXP-Simon
2026-02-12 15:41:07 +08:00
committed by Helian Nuits
parent e40781181c
commit 1576012851
2 changed files with 35 additions and 59 deletions
+26 -38
View File
@@ -61,56 +61,44 @@ 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
# 1. 剥离话题 ID (#)
target_without_topic = (
target_parent_id = (
target_simple_id.split("#", 1)[0]
if "#" in target_simple_id
else target_simple_id
)
# 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 匹配 (包含平台前缀)
def _is_match(
item: str,
target: str,
target_simple_id: str,
target_parent_id: str,
) -> bool:
if ":" in item:
if item == target:
return True
# 若 target 有平台前缀,尝试前缀匹配
if ":" in target:
try:
item_prefix, item_tail = item.rsplit(":", 1)
target_prefix, _ = target.rsplit(":", 1)
if item_prefix == target_prefix:
# 命中了清理后的群 ID (支持剥离话题或剥离独立会话前级)
if item_tail in (
target_without_topic,
target_actual_group_id,
):
return True
except (ValueError, IndexError):
pass
# 允许 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
# 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)
is_in_list = any(
_is_match(item, target, target_simple_id, target_parent_id)
for item in glist
)
if mode == "whitelist":
return is_in_list
@@ -69,18 +69,6 @@ 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(
@@ -109,7 +97,7 @@ class OneBotAdapter(PlatformAdapter):
# 调用 OneBot 标准 API: get_group_msg_history
result = await self.bot.call_action(
"get_group_msg_history",
group_id=self._parse_id(group_id),
group_id=int(group_id),
count=max_count,
)
@@ -353,7 +341,7 @@ class OneBotAdapter(PlatformAdapter):
await self.bot.call_action(
"send_group_msg",
group_id=self._parse_id(group_id),
group_id=int(group_id),
message=message,
)
return True
@@ -395,7 +383,7 @@ class OneBotAdapter(PlatformAdapter):
await self.bot.call_action(
"send_group_msg",
group_id=self._parse_id(group_id),
group_id=int(group_id),
message=message,
)
return True
@@ -423,7 +411,7 @@ class OneBotAdapter(PlatformAdapter):
try:
await self.bot.call_action(
"upload_group_file",
group_id=self._parse_id(group_id),
group_id=int(group_id),
file=file_path,
name=filename or file_path.replace("\\", "/").split("/")[-1],
)
@@ -459,7 +447,7 @@ class OneBotAdapter(PlatformAdapter):
await self.bot.call_action(
"send_group_forward_msg",
group_id=self._parse_id(group_id),
group_id=int(group_id),
messages=nodes,
)
return True
@@ -474,7 +462,7 @@ class OneBotAdapter(PlatformAdapter):
try:
result = await self.bot.call_action(
"get_group_info",
group_id=self._parse_id(group_id),
group_id=int(group_id),
)
if not result:
@@ -504,7 +492,7 @@ class OneBotAdapter(PlatformAdapter):
try:
result = await self.bot.call_action(
"get_group_member_list",
group_id=self._parse_id(group_id),
group_id=int(group_id),
)
members = []
@@ -531,8 +519,8 @@ class OneBotAdapter(PlatformAdapter):
try:
result = await self.bot.call_action(
"get_group_member_info",
group_id=self._parse_id(group_id),
user_id=self._parse_id(user_id),
group_id=int(group_id),
user_id=int(user_id),
)
if not result: