From 7aa1386a50870fffb478e45b4c281153930d2f98 Mon Sep 17 00:00:00 2001 From: Om Lanke Date: Thu, 1 Jun 2023 03:05:48 +0530 Subject: [PATCH 1/7] cooldowns and stuff for `SlashCommandGroup`s --- discord/commands/core.py | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 90a614a786..e9276b3c3e 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -294,18 +294,17 @@ async def prepare(self, ctx: ApplicationContext) -> None: f"The check functions for the command {self.name} failed" ) - if hasattr(self, "_max_concurrency"): - if self._max_concurrency is not None: - # For this application, context can be duck-typed as a Message - await self._max_concurrency.acquire(ctx) # type: ignore # ctx instead of non-existent message + if self._max_concurrency is not None: + # For this application, context can be duck-typed as a Message + await self._max_concurrency.acquire(ctx) # type: ignore # ctx instead of non-existent message - try: - self._prepare_cooldowns(ctx) - await self.call_before_hooks(ctx) - except: - if self._max_concurrency is not None: - await self._max_concurrency.release(ctx) # type: ignore # ctx instead of non-existent message - raise + try: + self._prepare_cooldowns(ctx) + await self.call_before_hooks(ctx) + except: + if self._max_concurrency is not None: + await self._max_concurrency.release(ctx) # type: ignore # ctx instead of non-existent message + raise def is_on_cooldown(self, ctx: ApplicationContext) -> bool: """Checks whether the command is currently on cooldown. @@ -1153,6 +1152,28 @@ def __init__( "description_localizations", MISSING ) + # exactly like ApplicationCommand + from ..ext.commands.cooldowns import BucketType, CooldownMapping, MaxConcurrency + + cooldown = getattr(self, "__commands_cooldown__", kwargs.get("cooldown")) + + if cooldown is None: + buckets = CooldownMapping(cooldown, BucketType.default) + elif isinstance(cooldown, CooldownMapping): + buckets = cooldown + else: + raise TypeError( + "Cooldown must be a an instance of CooldownMapping or None." + ) + + self._buckets: CooldownMapping = buckets + + max_concurrency = getattr( + self, "__commands_max_concurrency__", kwargs.get("max_concurrency") + ) + + self._max_concurrency: MaxConcurrency | None = max_concurrency + @property def module(self) -> str | None: return self.__module__ From 9d24aacad8174afd3c8d014107c8ccc2a28076af Mon Sep 17 00:00:00 2001 From: Om Lanke Date: Thu, 1 Jun 2023 03:05:48 +0530 Subject: [PATCH 2/7] cooldowns and stuff for `SlashCommandGroup`s --- discord/commands/core.py | 43 ++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 90a614a786..c16dd52c61 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -294,18 +294,17 @@ async def prepare(self, ctx: ApplicationContext) -> None: f"The check functions for the command {self.name} failed" ) - if hasattr(self, "_max_concurrency"): - if self._max_concurrency is not None: - # For this application, context can be duck-typed as a Message - await self._max_concurrency.acquire(ctx) # type: ignore # ctx instead of non-existent message + if self._max_concurrency is not None: + # For this application, context can be duck-typed as a Message + await self._max_concurrency.acquire(ctx) # type: ignore # ctx instead of non-existent message - try: - self._prepare_cooldowns(ctx) - await self.call_before_hooks(ctx) - except: - if self._max_concurrency is not None: - await self._max_concurrency.release(ctx) # type: ignore # ctx instead of non-existent message - raise + try: + self._prepare_cooldowns(ctx) + await self.call_before_hooks(ctx) + except: + if self._max_concurrency is not None: + await self._max_concurrency.release(ctx) # type: ignore # ctx instead of non-existent message + raise def is_on_cooldown(self, ctx: ApplicationContext) -> bool: """Checks whether the command is currently on cooldown. @@ -1119,6 +1118,8 @@ def __init__( description: str | None = None, guild_ids: list[int] | None = None, parent: SlashCommandGroup | None = None, + cooldown: Any = None, + max_concurrency: Any = None, **kwargs, ) -> None: self.name = str(name) @@ -1153,6 +1154,26 @@ def __init__( "description_localizations", MISSING ) + # just like ApplicationCommand + from ..ext.commands.cooldowns import BucketType, CooldownMapping, MaxConcurrency + + cooldown = getattr(self, "__commands_cooldown__", cooldown) + + if cooldown is None: + buckets = CooldownMapping(cooldown, BucketType.default) + elif isinstance(cooldown, CooldownMapping): + buckets = cooldown + else: + raise TypeError( + "Cooldown must be a an instance of CooldownMapping or None." + ) + + self._buckets: CooldownMapping = buckets + + max_concurrency = getattr(self, "__commands_max_concurrency__", max_concurrency) + + self._max_concurrency: MaxConcurrency | None = max_concurrency + @property def module(self) -> str | None: return self.__module__ From e8a18f652f38a5afff1a491c57d6a834b431374e Mon Sep 17 00:00:00 2001 From: Om1609 Date: Sat, 3 Jun 2023 00:20:46 +0530 Subject: [PATCH 3/7] remove unnecessary code --- discord/commands/core.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index c16dd52c61..831f62be74 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -1154,10 +1154,10 @@ def __init__( "description_localizations", MISSING ) - # just like ApplicationCommand + # similar to ApplicationCommand from ..ext.commands.cooldowns import BucketType, CooldownMapping, MaxConcurrency - cooldown = getattr(self, "__commands_cooldown__", cooldown) + # no need to getattr, since slash cmds groups cant be created using a decorator if cooldown is None: buckets = CooldownMapping(cooldown, BucketType.default) @@ -1170,7 +1170,14 @@ def __init__( self._buckets: CooldownMapping = buckets - max_concurrency = getattr(self, "__commands_max_concurrency__", max_concurrency) + # no need to getattr, since slash cmds groups cant be created using a decorator + + if max_concurrency is not None and not isinstance( + max_concurrency, MaxConcurrency + ): + raise TypeError( + "max_concurrency must be an instance of MaxConcurrency or None" + ) self._max_concurrency: MaxConcurrency | None = max_concurrency From 1b4bee9da10555c1a3434df0cc880bfe4c69b23b Mon Sep 17 00:00:00 2001 From: Om1609 Date: Sat, 3 Jun 2023 00:56:01 +0530 Subject: [PATCH 4/7] Changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5dcab1c7e0..1be9e088c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2042](https://github.com/Pycord-Development/pycord/pull/2042)) - Added `icon` and `unicode_emoji` to `Guild.create_role`. ([#2086](https://github.com/Pycord-Development/pycord/pull/2086)) +- Added `cooldown` and `max_concurrency` to`SlashCommandGroup`. + ([#2091](https://github.com/Pycord-Development/pycord/pull/2091)) ### Changed @@ -125,6 +127,8 @@ These changes are available on the `master` branch, but have not yet been releas ([#2079](https://github.com/Pycord-Development/pycord/pull/2079)) - Fixed `HTTPException` when trying to create a forum thread with files. ([#2075](https://github.com/Pycord-Development/pycord/pull/2075)) +- Fixed `before_invoke` not being run for `SlashCommandGroup`. + ([#2091](https://github.com/Pycord-Development/pycord/pull/2091)) ## [2.4.1] - 2023-03-20 From d9b90cac1d5f4e977346b04a5d1fb4bd91464f50 Mon Sep 17 00:00:00 2001 From: Om <92863779+Om1609@users.noreply.github.com> Date: Sun, 11 Jun 2023 11:22:46 +0530 Subject: [PATCH 5/7] Update CHANGELOG.md Co-authored-by: Middledot <78228142+Middledot@users.noreply.github.com> Signed-off-by: Om <92863779+Om1609@users.noreply.github.com> --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b9c2f000d..af8ce2663c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,7 +60,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#2042](https://github.com/Pycord-Development/pycord/pull/2042)) - Added `icon` and `unicode_emoji` to `Guild.create_role`. ([#2086](https://github.com/Pycord-Development/pycord/pull/2086)) -- Added `cooldown` and `max_concurrency` to`SlashCommandGroup`. +- Added `cooldown` and `max_concurrency` to `SlashCommandGroup`. ([#2091](https://github.com/Pycord-Development/pycord/pull/2091)) - Added new embedded activities, Gartic Phone and Jamspace. ([#2102](https://github.com/Pycord-Development/pycord/pull/2102)) From 9ebaa0a2bdae4867192030e34c642498117f0c63 Mon Sep 17 00:00:00 2001 From: Om Date: Mon, 12 Jun 2023 11:08:25 +0530 Subject: [PATCH 6/7] Typehint parameters --- discord/commands/core.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 831f62be74..264b25bfed 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -82,6 +82,8 @@ if TYPE_CHECKING: from typing_extensions import Concatenate, ParamSpec + from ..ext.commands.cooldowns import CooldownMapping, MaxConcurrency + from .. import Permissions from ..cog import Cog @@ -1118,8 +1120,8 @@ def __init__( description: str | None = None, guild_ids: list[int] | None = None, parent: SlashCommandGroup | None = None, - cooldown: Any = None, - max_concurrency: Any = None, + cooldown: CooldownMapping | None = None, + max_concurrency: MaxConcurrency | None = None, **kwargs, ) -> None: self.name = str(name) From d24250dd1d7c61941aa7400a638cca3d196af464 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 12 Jun 2023 05:38:48 +0000 Subject: [PATCH 7/7] style(pre-commit): auto fixes from pre-commit.com hooks --- discord/commands/core.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/discord/commands/core.py b/discord/commands/core.py index 264b25bfed..3f0018f88f 100644 --- a/discord/commands/core.py +++ b/discord/commands/core.py @@ -82,10 +82,9 @@ if TYPE_CHECKING: from typing_extensions import Concatenate, ParamSpec - from ..ext.commands.cooldowns import CooldownMapping, MaxConcurrency - from .. import Permissions from ..cog import Cog + from ..ext.commands.cooldowns import CooldownMapping, MaxConcurrency T = TypeVar("T") CogT = TypeVar("CogT", bound="Cog")