Skip to content

Commit 17eb534

Browse files
committed
turn is_on_cooldown_async
1 parent 9786798 commit 17eb534

File tree

3 files changed

+8
-19
lines changed

3 files changed

+8
-19
lines changed

discord/commands/core.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ async def prepare(self, ctx: ApplicationContext) -> None:
365365
await self._max_concurrency.release(ctx) # type: ignore # ctx instead of non-existent message
366366
raise
367367

368-
def is_on_cooldown(self, ctx: ApplicationContext) -> bool:
368+
async def is_on_cooldown(self, ctx: ApplicationContext) -> bool:
369369
"""Checks whether the command is currently on cooldown.
370370
371371
.. note::
@@ -385,7 +385,7 @@ def is_on_cooldown(self, ctx: ApplicationContext) -> bool:
385385
if not self._buckets.valid:
386386
return False
387387

388-
bucket = self._buckets.get_bucket(ctx) # type: ignore
388+
bucket = await self._buckets.get_bucket(ctx) # type: ignore
389389
current = utcnow().timestamp()
390390
return bucket.get_tokens(current) == 0
391391

@@ -920,9 +920,7 @@ def _match_option_param_names(self, params, options):
920920
def _is_typing_union(self, annotation):
921921
return getattr(annotation, "__origin__", None) is Union or type(
922922
annotation
923-
) is getattr(
924-
types, "UnionType", Union
925-
) # type: ignore
923+
) is getattr(types, "UnionType", Union) # type: ignore
926924

927925
def _is_typing_optional(self, annotation):
928926
return self._is_typing_union(annotation) and type(None) in annotation.__args__ # type: ignore

discord/ext/commands/cooldowns.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import discord.abc
3535
from discord.enums import Enum
36+
from discord import utils
3637

3738
from ...abc import PrivateChannel
3839
from .errors import MaxConcurrencyReached
@@ -307,15 +308,7 @@ def valid(self) -> bool:
307308
async def create_bucket(
308309
self, ctx: Context | ApplicationContext | Message
309310
) -> Cooldown:
310-
from ...ext.commands import Context
311-
312-
if isinstance(ctx, Context):
313-
result = self._factory(ctx.message)
314-
else:
315-
result = self._factory(ctx)
316-
if inspect.isawaitable(result):
317-
return await result
318-
return result
311+
return await utils.maybe_coroutine(self._factory, ctx)
319312

320313

321314
class _Semaphore:

discord/ext/commands/core.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ async def prepare(self, ctx: Context) -> None:
893893
def cooldown(self) -> Cooldown | None:
894894
return self._buckets._cooldown
895895

896-
def is_on_cooldown(self, ctx: Context) -> bool:
896+
async def is_on_cooldown(self, ctx: Context) -> bool:
897897
"""Checks whether the command is currently on cooldown.
898898
899899
Parameters
@@ -909,7 +909,7 @@ def is_on_cooldown(self, ctx: Context) -> bool:
909909
if not self._buckets.valid:
910910
return False
911911

912-
bucket = self._buckets.get_bucket(ctx.message)
912+
bucket = await self._buckets.get_bucket(ctx.message)
913913
dt = ctx.message.edited_at or ctx.message.created_at
914914
current = dt.replace(tzinfo=datetime.timezone.utc).timestamp()
915915
return bucket.get_tokens(current) == 0
@@ -1086,9 +1086,7 @@ def _is_typing_optional(self, annotation: T | T | None) -> TypeGuard[T | None]:
10861086
return (
10871087
getattr(annotation, "__origin__", None) is Union
10881088
or type(annotation) is getattr(types, "UnionType", Union)
1089-
) and type(
1090-
None
1091-
) in annotation.__args__ # type: ignore
1089+
) and type(None) in annotation.__args__ # type: ignore
10921090

10931091
@property
10941092
def signature(self) -> str:

0 commit comments

Comments
 (0)