| 123456789101112131415161718192021222324252627282930313233343536373839 |
- from __future__ import annotations
- from typing import Any
- from wbb.utils.dbadmin import get_managed_chat_settings, update_managed_chat_settings
- COMMAND_KEYS = {"giveaway", "checkin", "points", "history", "shortcuts"}
- DEFAULT_SETTINGS = {
- "cleanup_seconds": 10,
- "commands": {key: True for key in COMMAND_KEYS},
- }
- def normalize_interaction_settings(value: Any) -> dict[str, Any]:
- value = value if isinstance(value, dict) else {}
- seconds = int(value.get("cleanup_seconds", 10))
- if seconds < 1 or seconds > 300:
- raise ValueError("消息清理时间必须在 1 到 300 秒之间。")
- commands = value.get("commands") if isinstance(value.get("commands"), dict) else {}
- return {
- "cleanup_seconds": seconds,
- "commands": {key: bool(commands.get(key, True)) for key in sorted(COMMAND_KEYS)},
- }
- async def get_interaction_settings(chat_id: int) -> dict[str, Any]:
- settings = await get_managed_chat_settings(chat_id)
- return normalize_interaction_settings(settings.get("interaction_settings"))
- async def save_interaction_settings(chat_id: int, value: Any) -> dict[str, Any]:
- settings = normalize_interaction_settings(value)
- await update_managed_chat_settings(chat_id, {"interaction_settings": settings})
- return settings
- async def command_enabled(chat_id: int, key: str) -> bool:
- settings = await get_interaction_settings(chat_id)
- return bool(settings["commands"].get(key, True))
|