import time from typing import Dict, Optional import config class CooldownManager: """In-memory cooldown tracking for anti-spam.""" def __init__(self, cooldown_seconds: Optional[int] = None): self._cooldowns: Dict[int, float] = {} self.cooldown_seconds = cooldown_seconds or config.COMMISSION_COOLDOWN_SECONDS def check(self, user_id: int) -> Optional[int]: """ Check if a user is on cooldown. Returns: None if not on cooldown, otherwise seconds remaining. """ if user_id not in self._cooldowns: return None elapsed = time.time() - self._cooldowns[user_id] if elapsed >= self.cooldown_seconds: del self._cooldowns[user_id] return None return int(self.cooldown_seconds - elapsed) def set(self, user_id: int): """Set cooldown for a user.""" self._cooldowns[user_id] = time.time() def clear(self, user_id: int): """Clear cooldown for a user.""" self._cooldowns.pop(user_id, None) def is_on_cooldown(self, user_id: int) -> bool: """Check if user is currently on cooldown.""" return self.check(user_id) is not None