aboutsummaryrefslogtreecommitdiff
path: root/discord/client.py
diff options
context:
space:
mode:
authorRapptz <[email protected]>2015-12-29 11:59:18 -0500
committerRapptz <[email protected]>2015-12-29 11:59:18 -0500
commitc11bd9b8f4fa5b712249a6fcc97436edbfc5e022 (patch)
tree7ce37aa40745ec4eaf5cf71c9b9a59b0977fceb3 /discord/client.py
parentRaise ClientException if an unexpected websocket close happens (diff)
downloaddiscord.py-c11bd9b8f4fa5b712249a6fcc97436edbfc5e022.tar.xz
discord.py-c11bd9b8f4fa5b712249a6fcc97436edbfc5e022.zip
ConnectionState is now constructed in Client.__init__.
This should reduce the amount of checks for None if someone doesn't want a websocket connection. The connection state is instead cleared rather than reconstructed.
Diffstat (limited to 'discord/client.py')
-rw-r--r--discord/client.py22
1 files changed, 10 insertions, 12 deletions
diff --git a/discord/client.py b/discord/client.py
index e77d3b71..8a9dcd2e 100644
--- a/discord/client.py
+++ b/discord/client.py
@@ -109,15 +109,16 @@ class Client:
self.gateway = None
self.voice = None
self.session_id = None
- self.connection = None
self.sequence = 0
self.loop = asyncio.get_event_loop() if loop is None else loop
self._listeners = []
self.cache_auth = options.get('cache_auth', True)
- self.max_messages = options.get('max_messages')
- if self.max_messages is None or self.max_messages < 100:
- self.max_messages = 5000
+ max_messages = options.get('max_messages')
+ if max_messages is None or max_messages < 100:
+ max_messages = 5000
+
+ self.connection = ConnectionState(self.dispatch, max_messages)
# Blame React for this
user_agent = 'DiscordBot (https://github.com/Rapptz/discord.py {0}) Python/{1[0]}.{1[1]} aiohttp/{2}'
@@ -134,7 +135,7 @@ class Client:
# These two events correspond to the two events necessary
# for a connection to be made
self._voice_data_found = asyncio.Event(loop=self.loop)
- self._session_id_found = asyncio.Event(loop=self.loop)
+ self._session_id_found = asyncio.Event(loop=self.loop)
# internals
@@ -334,7 +335,7 @@ class Client:
event = msg.get('t')
if event == 'READY':
- self.connection = ConnectionState(self.dispatch, self.max_messages)
+ self.connection.clear()
self.session_id = data['session_id']
if event == 'READY' or event == 'RESUMED':
@@ -706,12 +707,11 @@ class Client:
while not self.is_closed:
msg = yield from self.ws.recv()
if msg is None:
- if self.connection is None:
- raise ClientException('Unexpected websocket closure received')
-
if self.ws.close_code == 1012:
yield from self.redirect_websocket(self.gateway)
continue
+ elif not self._is_ready.is_set():
+ raise ClientException('Unexpected websocket closure received')
else:
yield from self.close()
break
@@ -1743,9 +1743,7 @@ class Client:
# Invite management
def _fill_invite_data(self, data):
- server = None
- if self.connection is not None:
- server = self.connection._get_server(data['guild']['id'])
+ server = self.connection._get_server(data['guild']['id'])
if server is not None:
ch_id = data['channel']['id']
channels = getattr(server, 'channels', [])