From 2be65e5874d00e0b0209717ad8a4628dc9cea906 Mon Sep 17 00:00:00 2001 From: z03h <7235242+z03h@users.noreply.github.com> Date: Tue, 17 May 2022 00:37:31 -0700 Subject: [PATCH] [commands] Fix errors when DynamicCooldown returns None --- discord/ext/commands/cooldowns.py | 6 ++++-- discord/ext/commands/core.py | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/discord/ext/commands/cooldowns.py b/discord/ext/commands/cooldowns.py index fc57e22e0..26a0f5600 100644 --- a/discord/ext/commands/cooldowns.py +++ b/discord/ext/commands/cooldowns.py @@ -125,9 +125,9 @@ class CooldownMapping: def create_bucket(self, message: Message) -> Cooldown: return self._cooldown.copy() # type: ignore - def get_bucket(self, message: Message, current: Optional[float] = None) -> Cooldown: + def get_bucket(self, message: Message, current: Optional[float] = None) -> Optional[Cooldown]: if self._type is BucketType.default: - return self._cooldown # type: ignore + return self._cooldown self._verify_cache_integrity(current) key = self._bucket_key(message) @@ -142,6 +142,8 @@ class CooldownMapping: def update_rate_limit(self, message: Message, current: Optional[float] = None, tokens: int = 1) -> Optional[float]: bucket = self.get_bucket(message, current) + if bucket is None: + return None return bucket.update_rate_limit(current, tokens=tokens) diff --git a/discord/ext/commands/core.py b/discord/ext/commands/core.py index bfa496bb7..8b2daf60c 100644 --- a/discord/ext/commands/core.py +++ b/discord/ext/commands/core.py @@ -930,6 +930,8 @@ class Command(_BaseCommand, Generic[CogT, P, T]): return False bucket = self._buckets.get_bucket(ctx.message) + if bucket is None: + return False dt = ctx.message.edited_at or ctx.message.created_at current = dt.replace(tzinfo=datetime.timezone.utc).timestamp() return bucket.get_tokens(current) == 0 @@ -948,7 +950,8 @@ class Command(_BaseCommand, Generic[CogT, P, T]): """ if self._buckets.valid: bucket = self._buckets.get_bucket(ctx.message) - bucket.reset() + if bucket is not None: + bucket.reset() def get_cooldown_retry_after(self, ctx: Context[BotT], /) -> float: """Retrieves the amount of seconds before this command can be tried again. @@ -972,6 +975,8 @@ class Command(_BaseCommand, Generic[CogT, P, T]): """ if self._buckets.valid: bucket = self._buckets.get_bucket(ctx.message) + if bucket is None: + return 0.0 dt = ctx.message.edited_at or ctx.message.created_at current = dt.replace(tzinfo=datetime.timezone.utc).timestamp() return bucket.get_retry_after(current)