Browse Source

Optimise some member and user related routines.

* Skip user update if possible
* Drop copy.copy in favour of manual copy

Credit to Hornwitser for these findings.
pull/796/merge
Rapptz 7 years ago
parent
commit
095f0ec2fc
  1. 15
      discord/member.py
  2. 2
      discord/state.py
  3. 13
      discord/user.py

15
discord/member.py

@ -178,6 +178,20 @@ class Member(discord.abc.Messageable, _BaseUser):
def __hash__(self):
return hash(self._user)
@classmethod
def _copy(cls, member):
self = cls.__new__(cls) # to bypass __init__
self._roles = utils.SnowflakeList(member._roles, is_sorted=True)
self.joined_at = member.joined_at
self.status = member.status
self.guild = member.guild
self.nick = member.nick
self.activity = member.activity
self._state = member._state
self._user = User._copy(member._user)
return self
async def _get_channel(self):
ch = await self.create_dm()
return ch
@ -205,6 +219,7 @@ class Member(discord.abc.Messageable, _BaseUser):
self.status = try_enum(Status, data['status'])
self.activity = create_activity(data.get('game'))
if len(user) > 1:
u = self._user
u.name = user.get('username', u.name)
u.avatar = user.get('avatar', u.avatar)

2
discord/state.py

@ -449,7 +449,7 @@ class ConnectionState:
member = Member(guild=guild, data=data, state=self)
guild._add_member(member)
old_member = member._copy()
old_member = Member._copy(member)
member._presence_update(data=data, user=user)
self.dispatch('member_update', old_member, member)

13
discord/user.py

@ -91,6 +91,19 @@ class BaseUser(_BaseUser):
def __hash__(self):
return self.id >> 22
@classmethod
def _copy(cls, user):
self = cls.__new__(cls) # bypass __init__
self.name = user.name
self.id = user.id
self.discriminator = user.discriminator
self.avatar = user.avatar
self.bot = user.bot
self._state = user._state
return self
@property
def avatar_url(self):
"""Returns a friendly URL version of the avatar the user has.

Loading…
Cancel
Save