diff options
| author | Rapptz <[email protected]> | 2019-05-19 18:42:42 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2019-05-19 19:17:29 -0400 |
| commit | 82b54933e22417e68ae6b92cebefae98f5369518 (patch) | |
| tree | 0910afe21935b2287ec4e3f607e6542e34cc031c /discord/client.py | |
| parent | [tasks] Add version added note to Loop.change_interval (diff) | |
| download | discord.py-82b54933e22417e68ae6b92cebefae98f5369518.tar.xz discord.py-82b54933e22417e68ae6b92cebefae98f5369518.zip | |
Add asyncio.Task subclass for better __repr__ for events.
Diffstat (limited to 'discord/client.py')
| -rw-r--r-- | discord/client.py | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/discord/client.py b/discord/client.py index 9e6067a3..fd12d7ec 100644 --- a/discord/client.py +++ b/discord/client.py @@ -93,6 +93,22 @@ def _cleanup_loop(loop): log.info('Closing the event loop.') loop.close() +class _ClientEventTask(asyncio.Task): + def __init__(self, original_coro, event_name, coro, *, loop): + super().__init__(coro, loop=loop) + self.__event_name = event_name + self.__original_coro = original_coro + + def __repr__(self): + info = [ + ('state', self._state.lower()), + ('event', self.__event_name), + ('coro', repr(self.__original_coro)), + ] + if self._exception is not None: + info.append(('exception', repr(self._exception))) + return '<ClientEventTask {}>'.format(' '.join('%s=%s' % t for t in info)) + class Client: r"""Represents a client connection that connects to Discord. This class is used to interact with the Discord WebSocket and API. @@ -257,6 +273,11 @@ class Client: except asyncio.CancelledError: pass + def _schedule_event(self, coro, event_name, *args, **kwargs): + wrapped = self._run_event(coro, event_name, *args, **kwargs) + # Schedules the task + return _ClientEventTask(original_coro=coro, event_name=event_name, coro=wrapped, loop=self.loop) + def dispatch(self, event, *args, **kwargs): log.debug('Dispatching event %s', event) method = 'on_' + event @@ -295,7 +316,7 @@ class Client: except AttributeError: pass else: - asyncio.ensure_future(self._run_event(coro, method, *args, **kwargs), loop=self.loop) + self._schedule_event(coro, method, *args, **kwargs) async def on_error(self, event_method, *args, **kwargs): """|coro| |