Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```

</details>
Expand Down
7 changes: 7 additions & 0 deletions src/nonebot_plugin_parser/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Config(BaseModel):
"""Pilmoji 表情 CDN"""
parser_emoji_style: EmojiStyle = EmojiStyle.FACEBOOK
"""Pilmoji 表情样式"""
parser_group_blacklist_enabled: bool = True
"""是否启用群组黑名单模式(默认启用,即所有群聊的解析都是开启的)"""

@property
def nickname(self) -> str:
Expand Down Expand Up @@ -182,6 +184,11 @@ def emoji_style(self) -> EmojiStyle:
"""Pilmoji 表情样式"""
return self.parser_emoji_style

@property
def group_blacklist_enabled(self) -> bool:
"""是否启用群组黑名单模式"""
return self.parser_group_blacklist_enabled


pconfig: Config = get_plugin_config(Config)
"""插件配置"""
Expand Down
70 changes: 40 additions & 30 deletions src/nonebot_plugin_parser/matchers/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"


Expand All @@ -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.group_blacklist_enabled:
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.group_blacklist_enabled:
_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.group_blacklist_enabled:
_add_group(group_key)
else:
await matcher.finish("解析已关闭,无需重复关闭")
_remove_group(group_key)
await matcher.finish("解析已关闭")
Loading