From 8c896e9fbc52cb46f0d789570d64aacd369ffa06 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Sun, 29 Jan 2017 20:32:37 -0500 Subject: [PATCH] Re-add Client.wait_until_ready --- discord/client.py | 22 ++++++++++++++++++++++ examples/background_task.py | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/discord/client.py b/discord/client.py index 765090161..2e99b102a 100644 --- a/discord/client.py +++ b/discord/client.py @@ -120,6 +120,7 @@ class Client: self.connection.shard_count = self.shard_count self._closed = asyncio.Event(loop=self.loop) + self._ready = asyncio.Event(loop=self.loop) # if VoiceClient.warn_nacl: # VoiceClient.warn_nacl = False @@ -149,6 +150,9 @@ class Client: yield from self.ws.send_as_json(payload) + def handle_ready(self): + self._ready.set() + def _resolve_invite(self, invite): if isinstance(invite, Invite) or isinstance(invite, Object): return invite.id @@ -189,6 +193,11 @@ class Client: """List[:class:`VoiceClient`]: Represents a list of voice connections.""" return self.connection.voice_clients + @property + def is_ready(self): + """bool: Specifies if the client's internal cache is ready for use.""" + return self._ready.is_set() + @asyncio.coroutine def _run_event(self, coro, event_name, *args, **kwargs): try: @@ -356,6 +365,10 @@ class Client: except (ReconnectWebSocket, ResumeWebSocket) as e: resume = type(e) is ResumeWebSocket log.info('Got ' + type(e).__name__) + + if not resume: + self._ready.clear() + self.ws = yield from DiscordWebSocket.from_client(self, shard_id=self.shard_id, session=self.ws.session_id, sequence=self.ws.sequence, @@ -389,6 +402,7 @@ class Client: yield from self.http.close() self._closed.set() + self._ready.clear() @asyncio.coroutine def start(self, *args, **kwargs): @@ -513,6 +527,14 @@ class Client: # listeners/waiters + @asyncio.coroutine + def wait_until_ready(self): + """|coro| + + Waits until the client's internal cache is all ready. + """ + yield from self._ready.wait() + def wait_for(self, event, *, check=None, timeout=None): """|coro| diff --git a/examples/background_task.py b/examples/background_task.py index a7da6fc77..f6d7abcfa 100644 --- a/examples/background_task.py +++ b/examples/background_task.py @@ -15,7 +15,7 @@ class MyClient(discord.Client): print('------') async def my_background_task(self): - await self.wait_for('ready') + await self.wait_until_ready() counter = 0 channel = self.get_channel(1234567) # channel ID goes here while not self.is_closed: