Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixcoldown #74

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions core/redis_cooldown.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(

async def get_user_cooldown_residue(self, user_id: int, cooldown_in_seconds: int) -> float | None:
cooldown = await self.get_cooldown_start_at(user_id)
if not cooldown:
if not self._is_on_cooldown(cooldown, cooldown_in_seconds):
return None

cooldown_residue = (cooldown + cooldown_in_seconds) - time.time()
Expand All @@ -38,20 +38,17 @@ async def get_user_cooldown_residue(self, user_id: int, cooldown_in_seconds: int

async def get_cooldown_end_at(self, user_id: int, cooldown_in_seconds: int) -> float | None:
cooldown = await self.get_cooldown_start_at(user_id)
if not cooldown:
if not self._is_on_cooldown(cooldown, cooldown_in_seconds):
return None
return cooldown + cooldown_in_seconds
return cooldown

async def get_cooldown_start_at(self, user_id: int) -> float | None:
cooldown = await self._storage.get(user_id)
return cooldown

async def is_user_on_cooldown(self, user_id: int, cooldown_in_seconds: int) -> bool:
cooldown_end_at = await self.get_cooldown_end_at(user_id, cooldown_in_seconds)
now_time = datetime.datetime.now(tz=settings.TIMEZONE).timestamp()
if cooldown_end_at and cooldown_end_at > now_time:
return True
return False
return bool(cooldown_end_at)

async def set_user_cooldown(self, user_id: int, cooldown_in_seconds: int):
cooldown_expire = (
Expand All @@ -67,6 +64,15 @@ async def reset_cooldown(self, user_id: int):
await self._storage.delete(user_id)
logger.info(f'Cooldown for {user_id} has been reset manual')

def _is_on_cooldown(self, cooldown_start: float | None, cooldown_in_seconds: int) -> bool:
if not cooldown_start:
return False
now_time = datetime.datetime.now(tz=settings.TIMEZONE).timestamp()
end_at = cooldown_start + cooldown_in_seconds
if end_at > now_time:
return True
return False


class RedisLocaleCooldown:
"""
Expand Down
Loading