From 579dacb193df17423d95595d03488931f766669b Mon Sep 17 00:00:00 2001 From: fllesser Date: Tue, 30 Jun 2026 19:16:40 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat(config):=20=E6=B7=BB=E5=8A=A0=E7=BE=A4?= =?UTF-8?q?=E7=BB=84=E9=BB=91=E5=90=8D=E5=8D=95=E6=A8=A1=E5=BC=8F=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/nonebot_plugin_parser/config.py | 7 ++ src/nonebot_plugin_parser/matchers/filter.py | 70 +++++++++++--------- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/nonebot_plugin_parser/config.py b/src/nonebot_plugin_parser/config.py index a7da408a..b55926c8 100644 --- a/src/nonebot_plugin_parser/config.py +++ b/src/nonebot_plugin_parser/config.py @@ -56,6 +56,8 @@ class Config(BaseModel): """Pilmoji 表情 CDN""" parser_emoji_style: EmojiStyle = EmojiStyle.FACEBOOK """Pilmoji 表情样式""" + parser_blacklist_mode: bool = True + """是否启用黑名单模式(默认启用,所有群聊的解析都是开启的)""" @property def nickname(self) -> str: @@ -182,6 +184,11 @@ def emoji_style(self) -> EmojiStyle: """Pilmoji 表情样式""" return self.parser_emoji_style + @property + def blacklist_mode(self) -> bool: + """是否启用黑名单模式""" + return self.parser_blacklist_mode + pconfig: Config = get_plugin_config(Config) """插件配置""" diff --git a/src/nonebot_plugin_parser/matchers/filter.py b/src/nonebot_plugin_parser/matchers/filter.py index 43b51330..835c5223 100644 --- a/src/nonebot_plugin_parser/matchers/filter.py +++ b/src/nonebot_plugin_parser/matchers/filter.py @@ -10,30 +10,41 @@ from ..config import pconfig _DISABLED_GROUPS_PATH: Path = pconfig.data_dir / "disabled_groups.json" +_GROUP_SET_PATH: Path = pconfig.data_dir / "group_set.json" -def load_or_initialize_set() -> set[str]: - """加载或初始化关闭解析的名单""" +def _load_or_initialize_set() -> set[str]: + """加载群配置""" # 判断是否存在 - if not _DISABLED_GROUPS_PATH.exists(): - _DISABLED_GROUPS_PATH.write_text(json.dumps([])) - return set(json.loads(_DISABLED_GROUPS_PATH.read_text())) + if not _GROUP_SET_PATH.exists(): + if _DISABLED_GROUPS_PATH.exists(): + # 迁移旧的关闭解析名单 + _DISABLED_GROUPS_PATH.rename(_GROUP_SET_PATH) + else: + _GROUP_SET_PATH.write_text(json.dumps({})) + return set(json.loads(_GROUP_SET_PATH.read_text())) -def save_disabled_groups(): - """保存关闭解析的名单""" - _DISABLED_GROUPS_PATH.write_text(json.dumps(list(_DISABLED_GROUPS_SET))) +_GROUP_SET: set[str] = _load_or_initialize_set() -# 内存中关闭解析的名单,第一次先进行初始化 -_DISABLED_GROUPS_SET: set[str] = load_or_initialize_set() +def _save_group_set(): + _GROUP_SET_PATH.write_text(json.dumps(list(_GROUP_SET))) -def get_group_key(session: Session) -> str: - """获取群组的唯一标识符 - 由平台名称和会话场景 ID 组成,例如 `QQClient_123456789`。 - """ +def _add_group(group_key: str): + _GROUP_SET.add(group_key) + _save_group_set() + + +def _remove_group(group_key: str): + _GROUP_SET.discard(group_key) + _save_group_set() + + +def _get_group_key(session: Session) -> str: + """获取群组的唯一标识符 由平台名称和会话场景 ID 组成,例如 `QQClient_123456789`""" return f"{session.scope}_{session.scene_path}" @@ -42,31 +53,30 @@ def is_enabled(session: Session = UniSession()) -> bool: if session.scene.is_private: return True - group_key = get_group_key(session) - if group_key in _DISABLED_GROUPS_SET: - return False - return True + group_key = _get_group_key(session) + if pconfig.blacklist_mode: + return group_key not in _GROUP_SET + else: + return group_key in _GROUP_SET @on_command("开启解析", rule=to_me(), permission=SUPERUSER | ADMIN(), block=True).handle() async def _(matcher: Matcher, session: Session = UniSession()): """开启解析""" - group_key = get_group_key(session) - if group_key in _DISABLED_GROUPS_SET: - _DISABLED_GROUPS_SET.remove(group_key) - save_disabled_groups() - await matcher.finish("解析已开启") + group_key = _get_group_key(session) + if pconfig.blacklist_mode: + _remove_group(group_key) else: - await matcher.finish("解析已开启,无需重复开启") + _add_group(group_key) + await matcher.finish("解析已开启") @on_command("关闭解析", rule=to_me(), permission=SUPERUSER | ADMIN(), block=True).handle() async def _(matcher: Matcher, session: Session = UniSession()): """关闭解析""" - group_key = get_group_key(session) - if group_key not in _DISABLED_GROUPS_SET: - _DISABLED_GROUPS_SET.add(group_key) - save_disabled_groups() - await matcher.finish("解析已关闭") + group_key = _get_group_key(session) + if pconfig.blacklist_mode: + _add_group(group_key) else: - await matcher.finish("解析已关闭,无需重复关闭") + _remove_group(group_key) + await matcher.finish("解析已关闭") From 761a683d6d6387512fc6b8daed2c01ff4abb43c0 Mon Sep 17 00:00:00 2001 From: fllesser Date: Tue, 30 Jun 2026 19:23:48 +0800 Subject: [PATCH 2/3] rename and update readme --- README.md | 4 ++++ src/nonebot_plugin_parser/config.py | 10 +++++----- src/nonebot_plugin_parser/matchers/filter.py | 6 +++--- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 883fe4c6..c8ef0bc7 100644 --- a/README.md +++ b/README.md @@ -262,6 +262,10 @@ parser_emoji_cdn="https://emojicdn.elk.sh" # [可选] emoji 渲染样式 "apple", "google", "twitter", "facebook"(默认) parser_emoji_style="facebook" + +# [可选] 是否启用群组黑名单模式(默认启用,即所有群聊的解析都是开启的) +parser_group_blacklist_enabled=True + ``` diff --git a/src/nonebot_plugin_parser/config.py b/src/nonebot_plugin_parser/config.py index b55926c8..439ca0d7 100644 --- a/src/nonebot_plugin_parser/config.py +++ b/src/nonebot_plugin_parser/config.py @@ -56,8 +56,8 @@ class Config(BaseModel): """Pilmoji 表情 CDN""" parser_emoji_style: EmojiStyle = EmojiStyle.FACEBOOK """Pilmoji 表情样式""" - parser_blacklist_mode: bool = True - """是否启用黑名单模式(默认启用,所有群聊的解析都是开启的)""" + parser_group_blacklist_enabled: bool = True + """是否启用群组黑名单模式(默认启用,即所有群聊的解析都是开启的)""" @property def nickname(self) -> str: @@ -185,9 +185,9 @@ def emoji_style(self) -> EmojiStyle: return self.parser_emoji_style @property - def blacklist_mode(self) -> bool: - """是否启用黑名单模式""" - return self.parser_blacklist_mode + def group_blacklist_enabled(self) -> bool: + """是否启用群组黑名单模式""" + return self.parser_group_blacklist_enabled pconfig: Config = get_plugin_config(Config) diff --git a/src/nonebot_plugin_parser/matchers/filter.py b/src/nonebot_plugin_parser/matchers/filter.py index 835c5223..b0b69514 100644 --- a/src/nonebot_plugin_parser/matchers/filter.py +++ b/src/nonebot_plugin_parser/matchers/filter.py @@ -54,7 +54,7 @@ def is_enabled(session: Session = UniSession()) -> bool: return True group_key = _get_group_key(session) - if pconfig.blacklist_mode: + if pconfig.group_blacklist_enabled: return group_key not in _GROUP_SET else: return group_key in _GROUP_SET @@ -64,7 +64,7 @@ def is_enabled(session: Session = UniSession()) -> bool: async def _(matcher: Matcher, session: Session = UniSession()): """开启解析""" group_key = _get_group_key(session) - if pconfig.blacklist_mode: + if pconfig.group_blacklist_enabled: _remove_group(group_key) else: _add_group(group_key) @@ -75,7 +75,7 @@ async def _(matcher: Matcher, session: Session = UniSession()): async def _(matcher: Matcher, session: Session = UniSession()): """关闭解析""" group_key = _get_group_key(session) - if pconfig.blacklist_mode: + if pconfig.group_blacklist_enabled: _add_group(group_key) else: _remove_group(group_key) From 064623f769cb3d49e6007e0e44b3cbacfc51c7ca Mon Sep 17 00:00:00 2001 From: fllesser Date: Tue, 30 Jun 2026 19:33:00 +0800 Subject: [PATCH 3/3] fix --- src/nonebot_plugin_parser/matchers/filter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/nonebot_plugin_parser/matchers/filter.py b/src/nonebot_plugin_parser/matchers/filter.py index b0b69514..33333096 100644 --- a/src/nonebot_plugin_parser/matchers/filter.py +++ b/src/nonebot_plugin_parser/matchers/filter.py @@ -21,7 +21,7 @@ def _load_or_initialize_set() -> set[str]: # 迁移旧的关闭解析名单 _DISABLED_GROUPS_PATH.rename(_GROUP_SET_PATH) else: - _GROUP_SET_PATH.write_text(json.dumps({})) + _GROUP_SET_PATH.write_text(json.dumps([])) return set(json.loads(_GROUP_SET_PATH.read_text()))