diff options
| author | Rapptz <[email protected]> | 2017-04-12 21:26:23 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2017-04-12 21:26:23 -0400 |
| commit | 22150b20b6f571d91cd631a367ff62c65997d7de (patch) | |
| tree | 7d432b723417d23cc494b29217ceb998be448ae2 | |
| parent | Export missing enums. (diff) | |
| download | discord.py-22150b20b6f571d91cd631a367ff62c65997d7de.tar.xz discord.py-22150b20b6f571d91cd631a367ff62c65997d7de.zip | |
Start typing immediately when using async typing context manager.
| -rw-r--r-- | discord/context_managers.py | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/discord/context_managers.py b/discord/context_managers.py index a5f891b3..93d292c2 100644 --- a/discord/context_managers.py +++ b/discord/context_managers.py @@ -28,6 +28,13 @@ import asyncio from .compat import create_task +def _typing_done_callback(fut): + # just retrieve any exception and call it a day + try: + fut.exception() + except: + pass + class Typing: def __init__(self, messageable): self.loop = messageable._state.loop @@ -35,7 +42,11 @@ class Typing: @asyncio.coroutine def do_typing(self): - channel = yield from self.messageable._get_channel() + try: + channel = self._channel + except AttributeError: + channel = yield from self.messageable._get_channel() + typing = channel._state.http.send_typing while True: @@ -44,18 +55,18 @@ class Typing: def __enter__(self): self.task = create_task(self.do_typing(), loop=self.loop) + self.task.add_done_callback(_typing_done_callback) return self def __exit__(self, exc_type, exc, tb): - try: - self.task.cancel() - except: - pass + self.task.cancel() @asyncio.coroutine def __aenter__(self): + self._channel = channel = yield from self.messageable._get_channel() + yield from channel._state.http.send_typing(channel.id) return self.__enter__() @asyncio.coroutine def __aexit__(self, exc_type, exc, tb): - self.__exit__(exc_type, exc, tb) + self.task.cancel() |