aboutsummaryrefslogtreecommitdiff
path: root/utils/cooldowns.py
diff options
context:
space:
mode:
authorNatasha Moongrave <natasha@256phi.eu>2026-04-01 18:47:33 +0200
committerNatasha Moongrave <natasha@256phi.eu>2026-04-01 18:47:33 +0200
commit16733406364b182f90c0dd2de03ac4d3fa00d2a4 (patch)
treec2c7c8ee5f99c379dd0f4c0b353d37dc8189a656 /utils/cooldowns.py
parentdbe036aaf799ac31c3a8249ef7af07c7dc8c91d7 (diff)
Added partially finished things for extra functionalityHEADmaster
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