Skip to content

feat: cooldown upgrades #1323

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 11 commits into from
Apr 13, 2023
12 changes: 9 additions & 3 deletions interactions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
T_co,
utils,
)
from .client import const
from .models import (
ActionRow,
ActiveVoiceState,
Expand Down Expand Up @@ -328,10 +329,11 @@
WebhookMixin,
WebhookTypes,
WebSocketOPCode,
SlidingWindowSystem,
ExponentialBackoffSystem,
LeakyBucketSystem,
TokenBucketSystem,
)
from .api import events
from . import ext
from .client import const

__all__ = (
"__api_version__",
Expand Down Expand Up @@ -413,6 +415,10 @@
"cooldown",
"Cooldown",
"CooldownSystem",
"SlidingWindowSystem",
"ExponentialBackoffSystem",
"LeakyBucketSystem",
"TokenBucketSystem",
"CustomEmoji",
"CustomEmojiConverter",
"DateTrigger",
Expand Down
8 changes: 8 additions & 0 deletions interactions/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@
cooldown,
Cooldown,
CooldownSystem,
SlidingWindowSystem,
ExponentialBackoffSystem,
LeakyBucketSystem,
TokenBucketSystem,
CustomEmojiConverter,
DateTrigger,
dm_only,
Expand Down Expand Up @@ -361,6 +365,10 @@
"cooldown",
"Cooldown",
"CooldownSystem",
"SlidingWindowSystem",
"ExponentialBackoffSystem",
"LeakyBucketSystem",
"TokenBucketSystem",
"CustomEmoji",
"CustomEmojiConverter",
"DateTrigger",
Expand Down
15 changes: 14 additions & 1 deletion interactions/models/internal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,16 @@
UserConverter,
VoiceChannelConverter,
)
from .cooldowns import Buckets, Cooldown, CooldownSystem, MaxConcurrency
from .cooldowns import (
Buckets,
Cooldown,
CooldownSystem,
MaxConcurrency,
SlidingWindowSystem,
ExponentialBackoffSystem,
LeakyBucketSystem,
TokenBucketSystem,
)
from .listener import listen, Listener
from .protocols import Converter
from .extension import Extension
Expand Down Expand Up @@ -121,6 +130,10 @@
"cooldown",
"Cooldown",
"CooldownSystem",
"SlidingWindowSystem",
"ExponentialBackoffSystem",
"LeakyBucketSystem",
"TokenBucketSystem",
"CustomEmojiConverter",
"DateTrigger",
"dm_only",
Expand Down
10 changes: 6 additions & 4 deletions interactions/models/internal/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from interactions.client.utils.misc_utils import get_parameters, get_object_name, maybe_coroutine
from interactions.client.utils.serializer import no_export_meta
from interactions.models.internal.callback import CallbackObject
from interactions.models.internal.cooldowns import Cooldown, Buckets, MaxConcurrency
from interactions.models.internal.cooldowns import Cooldown, Buckets, MaxConcurrency, CooldownSystem
from interactions.models.internal.protocols import Converter

if TYPE_CHECKING:
Expand Down Expand Up @@ -283,19 +283,21 @@ def wrapper(coro: CommandT) -> CommandT:
return wrapper


def cooldown(bucket: Buckets, rate: int, interval: float) -> Callable[[CommandT], CommandT]:
def cooldown(
bucket: Buckets, rate: int, interval: float, cooldown_system: typing.Type[CooldownSystem] | None = None
) -> Callable[[CommandT], CommandT]:
"""
Add a cooldown to a command.

Args:
bucket: The bucket used to track cooldowns
rate: How many commands may be ran per interval
interval: How many seconds to wait for a cooldown

cooldown_system: The cooldown system to use
"""

def wrapper(coro: CommandT) -> CommandT:
cooldown_obj = Cooldown(bucket, rate, interval)
cooldown_obj = Cooldown(bucket, rate, interval, cooldown_system=cooldown_system)

coro.cooldown = cooldown_obj

Expand Down
Loading