diff options
| author | Steven Berler <[email protected]> | 2015-12-12 13:21:44 -0800 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2015-12-13 13:01:05 -0500 |
| commit | 037da750dd700fa3df6147e76d9f1344f6620a48 (patch) | |
| tree | c38393a68fa2f9b3d3d41b086eaf8ef13a137fcc | |
| parent | Remove uses of ClientSession. (diff) | |
| download | discord.py-037da750dd700fa3df6147e76d9f1344f6620a48.tar.xz discord.py-037da750dd700fa3df6147e76d9f1344f6620a48.zip | |
avoid potential bug when creating new private msgs
It probably isn't good to rely on an item that was added to a list to
still be the last item, especially if we could have other async
coroutines modify the list. This may not be an actual issue, but having
the function explicitly return the object that it just added to the list
should guarantee that we don't accidentally pull the wrong item from the
end of the list later.
| -rw-r--r-- | discord/client.py | 9 | ||||
| -rw-r--r-- | discord/state.py | 5 |
2 files changed, 8 insertions, 6 deletions
diff --git a/discord/client.py b/discord/client.py index b892d29d..0ce6e429 100644 --- a/discord/client.py +++ b/discord/client.py @@ -183,9 +183,8 @@ class Client: found = utils.find(lambda pm: pm.user == destination, self.private_channels) if found is None: # Couldn't find the user, so start a PM with them first. - yield from self.start_private_message(destination) - channel_id = self.private_channels[-1].id - return channel_id + channel = yield from self.start_private_message(destination) + return channel.id else: return found.id elif isinstance(destination, Object): @@ -732,7 +731,9 @@ class Client: yield from utils._verify_successful_response(r) data = yield from r.json() log.debug(request_success_log.format(response=r, json=payload, data=data)) - self.private_channels.append(PrivateChannel(id=data['id'], user=user)) + channel = PrivateChannel(id=data['id'], user=user) + self.private_channels.append(channel) + return channel @asyncio.coroutine def _rate_limit_helper(self, name, method, url, data): diff --git a/discord/state.py b/discord/state.py index 65ba008c..17b68314 100644 --- a/discord/state.py +++ b/discord/state.py @@ -55,6 +55,7 @@ class ConnectionState: def _add_server(self, guild): server = Server(**guild) self.servers.append(server) + return server def parse_ready(self, data): self.user = User(**data['user']) @@ -220,8 +221,8 @@ class ConnectionState: # unavailable during the READY event and is now # available, so it isn't in the cache... - self._add_server(data) - self.dispatch('server_join', self.servers[-1]) + server = self._add_server(data) + self.dispatch('server_join', server) def parse_guild_update(self, data): server = self._get_server(data.get('id')) |