Files

47 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
权限校验与规则解析模块
提供NoneBot命令的权限检查规则函数包括
- 群组权限检查(通用)
所有规则函数返回Rule对象用于NoneBot的matcher定义。
"""
from typing import Callable
from nonebot.rule import Rule
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, MessageEvent
def _check_group_allowed(config) -> Callable:
"""生成群组权限检查函数(内部复用,消除重复逻辑)。
Args:
config: Config实例需包含ALLOWED_GROUP_ID属性
Returns:
异步检查函数,私聊放行、群聊检查白名单
"""
async def _check(bot: Bot, event: MessageEvent) -> bool:
if not isinstance(event, GroupMessageEvent):
return True
# 单群模式:直接比较整数
return event.group_id == config.ALLOWED_GROUP_ID
return _check
def check_permission() -> Rule:
"""检查群组是否有权限使用抽卡功能。"""
from .config import Config
config = Config()
return Rule(_check_group_allowed(config))
def check_rank_permission() -> Rule:
"""检查用户是否有权限查看排行榜。
当前与check_permission逻辑相同保留为独立入口便于未来扩展。
"""
from .config import Config
config = Config()
return Rule(_check_group_allowed(config))