From 49d78c69c74cd06a43f43197e6ec119d37680ceb Mon Sep 17 00:00:00 2001 From: Rapptz Date: Thu, 17 Dec 2015 06:05:35 -0500 Subject: [PATCH] All update related events now pass in the previous state. --- discord/server.py | 4 +++- discord/state.py | 16 ++++++++++------ docs/api.rst | 16 ++++++++++------ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/discord/server.py b/discord/server.py index 0a6b7091f..566510e54 100644 --- a/discord/server.py +++ b/discord/server.py @@ -30,6 +30,7 @@ from .member import Member from .channel import Channel from .enums import ServerRegion, Status from .mixins import Hashable +import copy class Server(Hashable): """Represents a Discord server. @@ -94,11 +95,12 @@ class Server(Hashable): def _update_voice_state(self, data): user_id = data.get('user_id') member = utils.find(lambda m: m.id == user_id, self.members) + before = copy.copy(member) if member is not None: ch_id = data.get('channel_id') channel = utils.find(lambda c: c.id == ch_id, self.channels) member.update_voice_state(voice_channel=channel, **data) - return member + return before, member def _from_data(self, guild): self.name = guild.get('name') diff --git a/discord/state.py b/discord/state.py index 58b6b41b2..dddb4873d 100644 --- a/discord/state.py +++ b/discord/state.py @@ -148,8 +148,10 @@ class ConnectionState: if server is not None: channel_id = data.get('id') channel = utils.find(lambda c: c.id == channel_id, server.channels) - channel.update(server=server, **data) - self.dispatch('channel_update', channel) + if channel is not None: + old_channel = copy.copy(channel) + channel.update(server=server, **data) + self.dispatch('channel_update', old_channel, channel) def parse_channel_create(self, data): is_private = data.get('is_private', False) @@ -275,14 +277,16 @@ class ConnectionState: if server is not None: role_id = data['role']['id'] role = utils.find(lambda r: r.id == role_id, server.roles) - role.update(**data['role']) - self.dispatch('server_role_update', role) + if role is not None: + old_role = copy.copy(role) + role.update(**data['role']) + self.dispatch('server_role_update', old_role, role) def parse_voice_state_update(self, data): server = self._get_server(data.get('guild_id')) if server is not None: - updated_member = server._update_voice_state(data) - self.dispatch('voice_state_update', updated_member) + updated_members = server._update_voice_state(data) + self.dispatch('voice_state_update', *updated_members) def parse_typing_start(self, data): channel = self.get_channel(data.get('channel_id')) diff --git a/docs/api.rst b/docs/api.rst index 1a0903dee..6b4356c98 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -226,11 +226,12 @@ to handle it, which defaults to print a traceback and ignore the exception. :param channel: The :class:`Channel` that got added or deleted. -.. function:: on_channel_update(channel) +.. function:: on_channel_update(before, after) Called whenever a channel is updated. e.g. changed name, topic, permissions. - :param channel: The :class:`Channel` that got updated. + :param before: The :class:`Channel` that got updated with the old info. + :param after: The :class:`Channel` that got updated with the updated info. .. function:: on_member_join(member) on_member_remove(member) @@ -296,11 +297,12 @@ to handle it, which defaults to print a traceback and ignore the exception. :param server: The :class:`Server` that was created or deleted. :param role: The :class:`Role` that was created or deleted. -.. function:: on_server_role_update(role) +.. function:: on_server_role_update(before, after) Called when a :class:`Role` is changed server-wide. - :param role: The :class:`Role` that was updated. + :param before: The :class:`Role` that updated with the old info. + :param after: The :class:`Role` that updated with the updated info .. function:: on_server_available(server) on_server_unavailable(server) @@ -310,7 +312,7 @@ to handle it, which defaults to print a traceback and ignore the exception. :param server: The :class:`Server` that has changed availability. -.. function:: on_voice_state_update(member) +.. function:: on_voice_state_update(before, after) Called when a :class:`Member` changes their voice state. @@ -321,7 +323,8 @@ to handle it, which defaults to print a traceback and ignore the exception. - A member is muted or deafened by their own accord. - A member is muted or deafened by a server administrator. - :param member: The :class:`Member` whose voice state changed. + :param before: The :class:`Member` whose voice state changed prior to the changes. + :param after: The :class:`Member` whose voice state changed after the changes. .. function:: on_typing(channel, user, when) @@ -341,6 +344,7 @@ Utility Functions .. autofunction:: discord.utils.find +.. autofunction:: discord.utils.get Enumerators ------------