interaction_settings.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. from __future__ import annotations
  2. from typing import Any
  3. from wbb.utils.dbadmin import get_managed_chat_settings, update_managed_chat_settings
  4. COMMAND_KEYS = {"giveaway", "checkin", "points", "history", "shortcuts"}
  5. DEFAULT_SETTINGS = {
  6. "cleanup_seconds": 10,
  7. "commands": {key: True for key in COMMAND_KEYS},
  8. }
  9. def normalize_interaction_settings(value: Any) -> dict[str, Any]:
  10. value = value if isinstance(value, dict) else {}
  11. seconds = int(value.get("cleanup_seconds", 10))
  12. if seconds < 1 or seconds > 300:
  13. raise ValueError("消息清理时间必须在 1 到 300 秒之间。")
  14. commands = value.get("commands") if isinstance(value.get("commands"), dict) else {}
  15. return {
  16. "cleanup_seconds": seconds,
  17. "commands": {key: bool(commands.get(key, True)) for key in sorted(COMMAND_KEYS)},
  18. }
  19. async def get_interaction_settings(chat_id: int) -> dict[str, Any]:
  20. settings = await get_managed_chat_settings(chat_id)
  21. return normalize_interaction_settings(settings.get("interaction_settings"))
  22. async def save_interaction_settings(chat_id: int, value: Any) -> dict[str, Any]:
  23. settings = normalize_interaction_settings(value)
  24. await update_managed_chat_settings(chat_id, {"interaction_settings": settings})
  25. return settings
  26. async def command_enabled(chat_id: int, key: str) -> bool:
  27. settings = await get_interaction_settings(chat_id)
  28. return bool(settings["commands"].get(key, True))