From 3b11109d0f3c8a1d1da695c473d5df1d4746c811 Mon Sep 17 00:00:00 2001 From: UK <41271523+NeloBlivion@users.noreply.github.com> Date: Thu, 21 Mar 2024 00:30:23 +0000 Subject: [PATCH 1/3] add once kwarg to Cog.listener --- discord/cog.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/discord/cog.py b/discord/cog.py index 24a5b58e70..2e8a1d0e36 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -379,7 +379,7 @@ def _get_overridden_method(cls, method: FuncT) -> FuncT | None: ) @classmethod - def listener(cls, name: str = MISSING) -> Callable[[FuncT], FuncT]: + def listener(cls, name: str = MISSING, once: bool = False) -> Callable[[FuncT], FuncT]: """A decorator that marks a function as a listener. This is the cog equivalent of :meth:`.Bot.listen`. @@ -389,6 +389,9 @@ def listener(cls, name: str = MISSING) -> Callable[[FuncT], FuncT]: name: :class:`str` The name of the event being listened to. If not provided, it defaults to the function's name. + once: :class:`bool` + If this listener should only be called once after the cog is loaded. + Defaults to false. Raises ------ @@ -411,6 +414,7 @@ def decorator(func: FuncT) -> FuncT: raise TypeError("Listener function must be a coroutine function.") actual.__cog_listener__ = True to_assign = name or actual.__name__ + actual._once = once try: actual.__cog_listener_names__.append(to_assign) except AttributeError: From 0707ef650a7e5660e8175b326bfcb831aef51989 Mon Sep 17 00:00:00 2001 From: UK <41271523+NeloBlivion@users.noreply.github.com> Date: Thu, 21 Mar 2024 00:31:45 +0000 Subject: [PATCH 2/3] clarify --- discord/cog.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/cog.py b/discord/cog.py index 2e8a1d0e36..0157898058 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -390,7 +390,7 @@ def listener(cls, name: str = MISSING, once: bool = False) -> Callable[[FuncT], The name of the event being listened to. If not provided, it defaults to the function's name. once: :class:`bool` - If this listener should only be called once after the cog is loaded. + If this listener should only be called once after each cog load. Defaults to false. Raises From d6b7400f475020179246ae25fa13b2f467beb765 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 21 Mar 2024 01:06:44 +0000 Subject: [PATCH 3/3] style(pre-commit): auto fixes from pre-commit.com hooks --- discord/cog.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/discord/cog.py b/discord/cog.py index 0157898058..4f064edb26 100644 --- a/discord/cog.py +++ b/discord/cog.py @@ -379,7 +379,9 @@ def _get_overridden_method(cls, method: FuncT) -> FuncT | None: ) @classmethod - def listener(cls, name: str = MISSING, once: bool = False) -> Callable[[FuncT], FuncT]: + def listener( + cls, name: str = MISSING, once: bool = False + ) -> Callable[[FuncT], FuncT]: """A decorator that marks a function as a listener. This is the cog equivalent of :meth:`.Bot.listen`.