| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- from __future__ import annotations
- from typing import Any
- from pyrogram.types import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove
- from wbb import app
- from wbb.utils.dbpoints import (
- PointsError,
- get_point_rules,
- normalize_point_rules,
- set_point_rules,
- )
- CHECKIN_BUTTON_TEXT = "签到"
- def checkin_button_active(rules: dict[str, Any]) -> bool:
- return bool(
- rules.get("enabled")
- and rules.get("checkin_enabled")
- and rules.get("checkin_button_enabled")
- )
- def checkin_keyboard() -> ReplyKeyboardMarkup:
- return ReplyKeyboardMarkup(
- [[KeyboardButton(CHECKIN_BUTTON_TEXT)]],
- is_persistent=True,
- resize_keyboard=True,
- selective=False,
- placeholder="输入消息",
- )
- async def _sync_checkin_keyboard(chat_id: int, enabled: bool) -> None:
- if enabled:
- text = "签到快捷按钮已开启。点击下方“签到”即可领取每日积分。"
- reply_markup = checkin_keyboard()
- else:
- text = "签到快捷按钮已关闭。"
- reply_markup = ReplyKeyboardRemove(selective=False)
- try:
- await app.send_message(chat_id, text, reply_markup=reply_markup)
- except Exception as exc:
- raise PointsError(
- "无法更新群底部签到按钮,请确认机器人可以在群内发言。"
- ) from exc
- async def apply_point_rules(chat_id: int, values: dict[str, Any]) -> dict[str, Any]:
- current = await get_point_rules(chat_id)
- target = normalize_point_rules({**current, **values})
- current_active = checkin_button_active(current)
- target_active = checkin_button_active(target)
- saved = await set_point_rules(chat_id, target)
- if current_active == target_active:
- return saved
- try:
- await _sync_checkin_keyboard(chat_id, target_active)
- except Exception:
- await set_point_rules(chat_id, current)
- raise
- return saved
|