From 16733406364b182f90c0dd2de03ac4d3fa00d2a4 Mon Sep 17 00:00:00 2001 From: Natasha Moongrave Date: Wed, 1 Apr 2026 18:47:33 +0200 Subject: Added partially finished things for extra functionality --- utils/cooldowns.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 utils/cooldowns.py (limited to 'utils/cooldowns.py') diff --git a/utils/cooldowns.py b/utils/cooldowns.py new file mode 100644 index 0000000..a1dc153 --- /dev/null +++ b/utils/cooldowns.py @@ -0,0 +1,40 @@ +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 -- cgit v1.2.3