aboutsummaryrefslogtreecommitdiff
path: root/utils/cooldowns.py
diff options
context:
space:
mode:
Diffstat (limited to 'utils/cooldowns.py')
-rw-r--r--utils/cooldowns.py40
1 files changed, 40 insertions, 0 deletions
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