Files
DanDingNoneBot/danding_bot/plugins/danding_sign_in/rules.py
Mr.Xia 9ee5545cac feat(sign_in): 新增独立每日签到插件,与抽卡签到共用同一份签到数据
- 群聊命令「签到」(别名: 每日签到/打卡),白名单群聊或管理员可用
- 复用 xapi /bot/gacha/sign-in + /bot/points/add,与抽卡/WPF 共用 bot_gacha_daily_sign_in 表
- 积分随机 1-100,流水沿用 gacha_sign/抽卡签到;先到先得,重复签到提示已签到
- 新增 10 个单元测试,ruff 通过;更新 PLUGINS.md / README.md
2026-08-16 09:02:11 +08:00

60 lines
1.6 KiB
Python
Raw 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.

"""独立签到插件 - 权限校验。
签到命令仅在白名单群聊(或管理员)中可用;私聊非管理员不可用。
与 onmyoji_gacha 的规则不同:抽卡规则私聊放行,签到规则更严格(群聊白名单)。
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Optional
if TYPE_CHECKING:
from nonebot.rule import Rule
def is_allowed(
user_id: int,
group_id: Optional[int],
*,
allowed_group_id: int,
allowed_user_id: int,
) -> bool:
"""签到权限纯函数:管理员任意场景可用;其他用户仅白名单群聊可用。
Args:
user_id: 消息发送者 QQ
group_id: 群聊 ID非群聊场景为 None
allowed_group_id: 白名单群聊
allowed_user_id: 管理员 QQ
"""
if user_id == allowed_user_id:
return True
return group_id is not None and group_id == allowed_group_id
def check_permission() -> Rule:
"""生成签到命令权限 Rule白名单群聊 + 管理员)。
延迟导入 nonebot与 config.py 同模式),保证纯函数在无 nonebot 的
测试环境中可加载。
"""
from nonebot.rule import Rule
from nonebot.adapters.onebot.v11 import GroupMessageEvent, MessageEvent
from .config import Config
config = Config()
async def _checker(event: MessageEvent) -> bool:
group_id = event.group_id if isinstance(event, GroupMessageEvent) else None
return is_allowed(
event.user_id,
group_id,
allowed_group_id=config.ALLOWED_GROUP_ID,
allowed_user_id=config.ALLOWED_USER_ID,
)
return Rule(_checker)