From 6815caf80548098f9a6d21fa2f554a499148987d Mon Sep 17 00:00:00 2001 From: webjoin111 <455457521@qq.com> Date: Wed, 22 Oct 2025 18:22:14 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(tag):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E9=BB=91=E5=90=8D=E5=8D=95=E6=A0=87=E7=AD=BE=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E5=B9=B6=E4=BC=98=E5=8C=96=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E5=B1=95=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../builtin_plugins/superuser/tag_manage.py | 34 ++++++++++---- zhenxun/services/tags/manager.py | 46 +++++++++++++------ 2 files changed, 55 insertions(+), 25 deletions(-) diff --git a/zhenxun/builtin_plugins/superuser/tag_manage.py b/zhenxun/builtin_plugins/superuser/tag_manage.py index c7a57015..f934531b 100644 --- a/zhenxun/builtin_plugins/superuser/tag_manage.py +++ b/zhenxun/builtin_plugins/superuser/tag_manage.py @@ -213,17 +213,31 @@ async def handle_info(name: Match[str], bot: Bot): msg += f"模式: {mode}\n" msg += f"描述: {details['description'] or '无'}\n" - if details["tag_type"] == "DYNAMIC": + if details["tag_type"] == "STATIC" and details["is_blacklist"]: + msg += f"排除群组 ({len(details['groups'])}个):\n" + if details["groups"]: + msg += "\n".join(f"- {gid}" for gid in details["groups"]) + else: + msg += "无" + msg += "\n\n" + + if details["tag_type"] == "DYNAMIC" and details.get("dynamic_rule"): msg += f"动态规则: {details['dynamic_rule']}\n" - if details["resolved_groups"] is not None: - msg += f"当前匹配群组 ({len(details['resolved_groups'])}个):\n" - if details["resolved_groups"]: - msg += "\n".join( - f"- {g_name} ({g_id})" - for g_id, g_name in details["resolved_groups"] - ) - else: - msg += "无" + + title = ( + "当前生效群组" + if details["tag_type"] == "DYNAMIC" or details["is_blacklist"] + else "关联群组" + ) + + if details["resolved_groups"] is not None: + msg += f"{title} ({len(details['resolved_groups'])}个):\n" + if details["resolved_groups"]: + msg += "\n".join( + f"- {g_name} ({g_id})" for g_id, g_name in details["resolved_groups"] + ) + else: + msg += "无" else: msg += f"关联群组 ({len(details['groups'])}个):\n" if details["groups"]: diff --git a/zhenxun/services/tags/manager.py b/zhenxun/services/tags/manager.py index 012a85dd..8c914baa 100644 --- a/zhenxun/services/tags/manager.py +++ b/zhenxun/services/tags/manager.py @@ -234,16 +234,13 @@ class TagManager: if not tag: return None - resolved_groups = None - if tag.tag_type == "DYNAMIC" and bot: - resolved_group_ids = await self.resolve_tag_to_group_ids(name, bot=bot) - if resolved_group_ids: - groups_from_db = await GroupConsole.filter( - group_id__in=resolved_group_ids - ).all() - resolved_groups = [(g.group_id, g.group_name) for g in groups_from_db] - else: - resolved_groups = [] + final_group_ids = await self.resolve_tag_to_group_ids(name, bot=bot) + resolved_groups: list[tuple[str, str]] = [] + if final_group_ids: + groups_from_db = await GroupConsole.filter( + group_id__in=final_group_ids + ).all() + resolved_groups = [(g.group_id, g.group_name) for g in groups_from_db] return { "name": tag.name, @@ -418,18 +415,37 @@ class TagManager: if not tag: return [] - if tag.tag_type == "DYNAMIC": + associated_groups: set[str] = set() + if tag.tag_type == "STATIC": + associated_groups = {str(link.group_id) for link in tag.groups} + elif tag.tag_type == "DYNAMIC": if not tag.dynamic_rule or not isinstance(tag.dynamic_rule, dict | str): return [] - associated_groups = await self._resolve_dynamic_tag(tag.dynamic_rule, bot) + dynamic_ids = await self._resolve_dynamic_tag(tag.dynamic_rule, bot) + associated_groups = {str(gid) for gid in dynamic_ids} else: - associated_groups = {link.group_id for link in tag.groups} + associated_groups = {str(link.group_id) for link in tag.groups} if tag.is_blacklist: - all_group_ids_from_db = await GroupConsole.all().values_list( + all_groups_query = GroupConsole.all() + if bot: + bot_groups, _ = await PlatformUtils.get_group_list(bot) + bot_group_ids = {str(g.group_id) for g in bot_groups if g.group_id} + if bot_group_ids: + all_groups_query = all_groups_query.filter( + group_id__in=bot_group_ids + ) + else: + return [] + + all_relevant_group_ids_from_db = await all_groups_query.values_list( "group_id", flat=True ) - return list({str(gid) for gid in all_group_ids_from_db} - associated_groups) + all_relevant_group_ids = { + str(gid) for gid in all_relevant_group_ids_from_db + } + + return list(all_relevant_group_ids - associated_groups) else: return list(associated_groups)