diff --git a/discord/member.py b/discord/member.py index b3961cf27..6107038de 100644 --- a/discord/member.py +++ b/discord/member.py @@ -189,6 +189,17 @@ class Member(discord.abc.Messageable, _BaseUser): } return cls(data=data, guild=message.guild, state=message._state) + @classmethod + def _from_presence_update(cls, *, data, guild, state): + clone = cls(data=data, guild=guild, state=state) + to_return = cls(data=data, guild=guild, state=state) + to_return._client_status = { + key: value + for key, value in data.get('client_status', {}).items() + } + to_return._client_status[None] = data['status'] + return to_return, clone + @classmethod def _copy(cls, member): self = cls.__new__(cls) # to bypass __init__ @@ -200,7 +211,10 @@ class Member(discord.abc.Messageable, _BaseUser): self.nick = member.nick self.activities = member.activities self._state = member._state - self._user = User._copy(member._user) + + # Reference will not be copied unless necessary by PRESENCE_UPDATE + # See below + self._user = member._user return self async def _get_channel(self): @@ -230,9 +244,15 @@ class Member(discord.abc.Messageable, _BaseUser): if len(user) > 1: u = self._user - u.name = user.get('username', u.name) - u.avatar = user.get('avatar', u.avatar) - u.discriminator = user.get('discriminator', u.discriminator) + original = (u.name, u.avatar, u.discriminator) + # These keys seem to always be available + modified = (user['username'], user['avatar'], user['discriminator']) + if original != modified: + to_return = User._copy(self._user) + u.name, u.avatar, u.discriminator = modified + # Signal to dispatch on_user_update + return to_return, u + return False @property def status(self): diff --git a/discord/state.py b/discord/state.py index ab3f2ac7b..3d420a0a6 100644 --- a/discord/state.py +++ b/discord/state.py @@ -461,11 +461,14 @@ class ConnectionState: # skip these useless cases. return - member = Member(guild=guild, data=data, state=self) + member, old_member = Member._from_presence_update(guild=guild, data=data, state=self) guild._add_member(member) + else: + old_member = Member._copy(member) + user_update = member._presence_update(data=data, user=user) + if user_update: + self.dispatch('user_update', user_update[0], user_update[1]) - old_member = Member._copy(member) - member._presence_update(data=data, user=user) self.dispatch('member_update', old_member, member) def parse_user_update(self, data): diff --git a/docs/api.rst b/docs/api.rst index 7371537da..5f1466e54 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -439,13 +439,25 @@ to handle it, which defaults to print a traceback and ignoring the exception. - status - game playing - - avatar - nickname - roles :param before: The :class:`Member` that updated their profile with the old info. :param after: The :class:`Member` that updated their profile with the updated info. +.. function:: on_user_update(before, after) + + Called when a :class:`User` updates their profile. + + This is called when one or more of the following things change: + + - avatar + - username + - discriminator + + :param before: The :class:`User` that updated their profile with the old info. + :param after: The :class:`User` that updated their profile with the updated info. + .. function:: on_guild_join(guild) Called when a :class:`Guild` is either created by the :class:`Client` or when the