From 614f79409ca3fb1e335e4ed8f900f2dbbe8b3b2c Mon Sep 17 00:00:00 2001 From: Om <92863779+Om1609@users.noreply.github.com> Date: Fri, 31 Mar 2023 20:05:21 +0530 Subject: [PATCH] fix: attribute error with listeners in cogs (#1989) * fix attribute error Signed-off-by: Om <92863779+Om1609@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Revert "[pre-commit.ci] auto fixes from pre-commit.com hooks" This reverts commit 3375d386bc662d9ddca6c60ecf620dbc00c0a3b3. * Revert "fix attribute error" This reverts commit b8733f2a549c51551a556ccd16f7af75573bc936. * fix using EAPF * Changelog and comments * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update CHANGELOG.md Signed-off-by: Om <92863779+Om1609@users.noreply.github.com> * Update CHANGELOG.md Co-authored-by: BobDotCom <71356958+BobDotCom@users.noreply.github.com> Signed-off-by: Om <92863779+Om1609@users.noreply.github.com> --------- Signed-off-by: Om <92863779+Om1609@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: BobDotCom <71356958+BobDotCom@users.noreply.github.com> --- CHANGELOG.md | 6 ++++++ discord/client.py | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cb78469987..dd044d7072 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,12 @@ These changes are available on the `master` branch, but have not yet been releas - Removed `@client.once()` in favour of `@client.listen(once=True)`. ([#1957](https://github.com/Pycord-Development/pycord/pull/1957)) +### Fixed + +- Fixed `AttributeError` caused by + [#1957](https://github.com/Pycord-Development/pycord/pull/1957) when using listeners + in cogs. ([#1989](https://github.com/Pycord-Development/pycord/pull/1989)) + ## [2.4.1] - 2023-03-20 ### Changed diff --git a/discord/client.py b/discord/client.py index a6849a205b..654219a0bd 100644 --- a/discord/client.py +++ b/discord/client.py @@ -442,8 +442,18 @@ def dispatch(self, event: str, *args: Any, **kwargs: Any) -> None: # Schedule additional handlers registered with @listen for coro in self._event_handlers.get(method, []): self._schedule_event(coro, method, *args, **kwargs) - if coro._once: - once_listeners.append(coro) + + try: + if coro._once: # added using @listen() + once_listeners.append(coro) + + except AttributeError: # added using @Cog.add_listener() + # https://github.com/Pycord-Development/pycord/pull/1989 + # Although methods are similar to functions, attributes can't be added to them. + # This means that we can't add the `_once` attribute in the `add_listener` method + # and can only be added using the `@listen` decorator. + + continue # remove the once listeners for coro in once_listeners: