Skip to content

feat: allow getting a cooldown with just an id #1337

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

Merged
merged 1 commit into from
Apr 11, 2023
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
24 changes: 24 additions & 0 deletions interactions/models/internal/cooldowns.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import time
import typing
from enum import IntEnum
from typing import TYPE_CHECKING, Any, Dict

Expand Down Expand Up @@ -64,6 +65,12 @@ def __init__(self, cooldown_bucket: Buckets, rate: int, interval: float) -> None
self.interval: float = interval

async def get_cooldown(self, context: "BaseContext") -> "CooldownSystem":
"""
Get the cooldown system for the command.

Args:
context: The context of the command
"""
key = await self.bucket(context)

if key not in self.cooldown_repositories:
Expand All @@ -72,6 +79,23 @@ async def get_cooldown(self, context: "BaseContext") -> "CooldownSystem":
return cooldown
return self.cooldown_repositories.get(await self.bucket(context))

def get_cooldown_with_key(self, key: Any, *, create: bool = False) -> typing.Optional["CooldownSystem"]:
"""
Get the cooldown system for the command.

Note:
The preferred way to get the cooldown system is to use `get_cooldown` as it will use the context to get the correct key.

Args:
key: The key to get the cooldown system for
create: Whether to create a new cooldown system if one does not exist
"""
if key not in self.cooldown_repositories and create:
cooldown = CooldownSystem(self.rate, self.interval)
self.cooldown_repositories[key] = cooldown
return cooldown
return self.cooldown_repositories.get(key)

async def acquire_token(self, context: "BaseContext") -> bool:
"""
Attempt to acquire a token for a command to run. Uses the context of the command to use the correct CooldownSystem.
Expand Down