aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRapptz <[email protected]>2017-01-29 20:32:37 -0500
committerRapptz <[email protected]>2017-01-29 20:33:48 -0500
commit8c896e9fbc52cb46f0d789570d64aacd369ffa06 (patch)
tree3b10a7945613e6fca12478cb506dfbf5dd3089eb
parentAllow removing an embed in Message.edit (diff)
downloaddiscord.py-8c896e9fbc52cb46f0d789570d64aacd369ffa06.tar.xz
discord.py-8c896e9fbc52cb46f0d789570d64aacd369ffa06.zip
Re-add Client.wait_until_ready
-rw-r--r--discord/client.py22
-rw-r--r--examples/background_task.py2
2 files changed, 23 insertions, 1 deletions
diff --git a/discord/client.py b/discord/client.py
index 76509016..2e99b102 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 a7da6fc7..f6d7abcf 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: