From 43d90147a78efb4ab832b84442d68557fe48a3b7 Mon Sep 17 00:00:00 2001 From: Om1609 Date: Sat, 4 Mar 2023 01:18:53 +0530 Subject: [PATCH 1/6] reimplement once time event listeners --- discord/client.py | 71 ++++++++--------------------------------------- 1 file changed, 12 insertions(+), 59 deletions(-) diff --git a/discord/client.py b/discord/client.py index 07bba3bf41..c8d6295b98 100644 --- a/discord/client.py +++ b/discord/client.py @@ -435,9 +435,19 @@ def dispatch(self, event: str, *args: Any, **kwargs: Any) -> None: else: self._schedule_event(coro, method, *args, **kwargs) + # collect the once listeners as removing them from the list + # while iterating over it causes issues + once_listeners = [] + # 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) + + # remove the once listeners + for coro in once_listeners: + self._event_handlers[method].remove(coro) async def on_error(self, event_method: str, *args: Any, **kwargs: Any) -> None: """|coro| @@ -1204,7 +1214,7 @@ def remove_listener(self, func: Coro, name: str = MISSING) -> None: except ValueError: pass - def listen(self, name: str = MISSING) -> Callable[[Coro], Coro]: + def listen(self, name: str = MISSING, once: bool = False) -> Callable[[Coro], Coro]: """A decorator that registers another function as an external event listener. Basically this allows you to listen to multiple events from different places e.g. such as :func:`.on_ready` @@ -1241,6 +1251,7 @@ def decorator(func: Coro) -> Coro: if name == "on_application_command_error": return self.event(func) + func._once = once self.add_listener(func, name) return func @@ -1286,64 +1297,6 @@ async def on_ready(): _log.debug("%s has successfully been registered as an event", coro.__name__) return coro - def once( - self, name: str = MISSING, check: Callable[..., bool] | None = None - ) -> Coro: - """A decorator that registers an event to listen to only once. - - You can find more info about the events on the :ref:`documentation below `. - - The events must be a :ref:`coroutine `, if not, :exc:`TypeError` is raised. - - Parameters - ---------- - name: :class:`str` - The name of the event we want to listen to. This is passed to - :py:meth:`~discord.Client.wait_for`. Defaults to ``func.__name__``. - check: Optional[Callable[..., :class:`bool`]] - A predicate to check what to wait for. The arguments must meet the - parameters of the event being waited for. - - Raises - ------ - TypeError - The coroutine passed is not actually a coroutine. - - Example - ------- - - .. code-block:: python3 - - @client.once() - async def ready(): - print('Ready!') - """ - - def decorator(func: Coro) -> Coro: - if not asyncio.iscoroutinefunction(func): - raise TypeError("event registered must be a coroutine function") - - async def wrapped() -> None: - nonlocal name - nonlocal check - - name = func.__name__ if name is MISSING else name - - args = await self.wait_for(name, check=check) - - arg_len = func.__code__.co_argcount - if arg_len == 0 and args is None: - await func() - elif arg_len == 1: - await func(args) - else: - await func(*args) - - self.loop.create_task(wrapped()) - return func - - return decorator - async def change_presence( self, *, From 09360da150b6c1608da7595c3a1a0d7f8a821e72 Mon Sep 17 00:00:00 2001 From: Om1609 Date: Sat, 4 Mar 2023 01:29:07 +0530 Subject: [PATCH 2/6] docs for once parameter --- discord/client.py | 5 +++++ docs/api/clients.rst | 11 ++++------- docs/ext/commands/api.rst | 7 ++----- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/discord/client.py b/discord/client.py index c8d6295b98..a6849a205b 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1243,6 +1243,11 @@ async def on_message(message): async def my_message(message): print('two') + # listen to the first event only + @client.listen('on_ready', once=True) + async def on_ready(): + print('ready!') + Would print one and two in an unspecified order. """ diff --git a/docs/api/clients.rst b/docs/api/clients.rst index aeae87cbcb..f5b03d8e6c 100644 --- a/docs/api/clients.rst +++ b/docs/api/clients.rst @@ -10,7 +10,7 @@ Bots .. autoclass:: Bot :members: :inherited-members: - :exclude-members: command, event, message_command, slash_command, user_command, listen, once + :exclude-members: command, event, message_command, slash_command, user_command, listen .. automethod:: Bot.command(**kwargs) :decorator: @@ -27,10 +27,7 @@ Bots .. automethod:: Bot.user_command(**kwargs) :decorator: - .. automethod:: Bot.listen(name=None) - :decorator: - - .. automethod:: Bot.once(name=None, check=None) + .. automethod:: Bot.listen(name=None, once=False) :decorator: .. attributetable:: AutoShardedBot @@ -44,7 +41,7 @@ Clients .. attributetable:: Client .. autoclass:: Client :members: - :exclude-members: fetch_guilds, event, once + :exclude-members: fetch_guilds, event, listen .. automethod:: Client.event() :decorator: @@ -52,7 +49,7 @@ Clients .. automethod:: Client.fetch_guilds :async-for: - .. automethod:: Client.once(name=None, check=None) + .. automethod:: Client.listen(name=None, once=False) :decorator: .. attributetable:: AutoShardedClient diff --git a/docs/ext/commands/api.rst b/docs/ext/commands/api.rst index 119a8de10c..2d9af7f378 100644 --- a/docs/ext/commands/api.rst +++ b/docs/ext/commands/api.rst @@ -23,7 +23,7 @@ Bot .. autoclass:: discord.ext.commands.Bot :members: :inherited-members: - :exclude-members: after_invoke, before_invoke, check, check_once, command, event, group, listen, once + :exclude-members: after_invoke, before_invoke, check, check_once, command, event, group, listen .. automethod:: Bot.after_invoke() :decorator: @@ -46,10 +46,7 @@ Bot .. automethod:: Bot.group(*args, **kwargs) :decorator: - .. automethod:: Bot.listen(name=None) - :decorator: - - .. automethod:: Bot.once(name=None, check=None) + .. automethod:: Bot.listen(name=None, once=False) :decorator: AutoShardedBot From a0b7ebb5bd2091f39ce6c917805272e90d178742 Mon Sep 17 00:00:00 2001 From: Om1609 Date: Sat, 4 Mar 2023 01:39:42 +0530 Subject: [PATCH 3/6] Changelog --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c717c55347..46888e7d67 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,8 +15,8 @@ These changes are available on the `master` branch, but have not yet been releas - Added new events `on_bridge_command`, `on_bridge_command_completion`, and `on_bridge_command_error`. ([#1916](https://github.com/Pycord-Development/pycord/pull/1916)) -- Added the `@client.once()` decorator, which serves as a one-time event listener. - ([#1940](https://github.com/Pycord-Development/pycord/pull/1940)) +- Added support for one-time event listeners in `@client.listen()`. + ([#1957](https://github.com/Pycord-Development/pycord/pull/1957)) - Added support for text-related features in `StageChannel` ([#1936](https://github.com/Pycord-Development/pycord/pull/1936)) From 57740bd5ab505e1f626862d9818b07e1f0fe86ee Mon Sep 17 00:00:00 2001 From: Om <92863779+Om1609@users.noreply.github.com> Date: Sun, 5 Mar 2023 01:58:47 +0530 Subject: [PATCH 4/6] Update CHANGELOG.md Signed-off-by: Om <92863779+Om1609@users.noreply.github.com> --- CHANGELOG.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46888e7d67..8df2be7020 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,10 +15,12 @@ These changes are available on the `master` branch, but have not yet been releas - Added new events `on_bridge_command`, `on_bridge_command_completion`, and `on_bridge_command_error`. ([#1916](https://github.com/Pycord-Development/pycord/pull/1916)) -- Added support for one-time event listeners in `@client.listen()`. - ([#1957](https://github.com/Pycord-Development/pycord/pull/1957)) +- Added the `@client.once()` decorator, which serves as a one-time event listener. + ([#1940](https://github.com/Pycord-Development/pycord/pull/1940)) - Added support for text-related features in `StageChannel` ([#1936](https://github.com/Pycord-Development/pycord/pull/1936)) +- Added support for one-time event listeners in `@client.listen()`. + ([#1957](https://github.com/Pycord-Development/pycord/pull/1957)) ### Fixed @@ -27,6 +29,10 @@ These changes are available on the `master` branch, but have not yet been releas - Fixed the voice ip discovery due to the recent [announced change](https://discord.com/channels/613425648685547541/697138785317814292/1080623873629884486). ([#1955](https://github.com/Pycord-Development/pycord/pull/1955)) + +### Removed +- Removed `@client.once()` in favour of `@client.listen(once=True)` + ([#1957](https://github.com/Pycord-Development/pycord/pull/1957)) ## [2.4.0] - 2023-02-10 From ec5fbf67e17e7ba0121914180d797a2b423d1b9b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 4 Mar 2023 20:29:08 +0000 Subject: [PATCH 5/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8df2be7020..ed28ef7a11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,8 +29,9 @@ These changes are available on the `master` branch, but have not yet been releas - Fixed the voice ip discovery due to the recent [announced change](https://discord.com/channels/613425648685547541/697138785317814292/1080623873629884486). ([#1955](https://github.com/Pycord-Development/pycord/pull/1955)) - + ### Removed + - Removed `@client.once()` in favour of `@client.listen(once=True)` ([#1957](https://github.com/Pycord-Development/pycord/pull/1957)) From 6b9060465ad28aeb652a27b09b541cc1056f8b25 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 25 Mar 2023 07:33:48 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f0cf910f..b763c9c413 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,7 +23,7 @@ These changes are available on the `master` branch, but have not yet been releas ([#1957](https://github.com/Pycord-Development/pycord/pull/1957)) - Added `current_page` argument to Paginator.update() ([#1983](https://github.com/Pycord-Development/pycord/pull/1983) - + ### Removed - Removed `@client.once()` in favour of `@client.listen(once=True)`