aboutsummaryrefslogtreecommitdiff
path: root/utils/cooldowns.py
blob: a1dc1535d3f99a7a523c0e4e82d05c04ba0ac2a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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