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. 23
      discord/member.py
  2. 2
      discord/state.py
  3. 13
      discord/user.py

23
discord/member.py

@ -178,6 +178,20 @@ class Member(discord.abc.Messageable, _BaseUser):
def __hash__(self): def __hash__(self):
return hash(self._user) 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): async def _get_channel(self):
ch = await self.create_dm() ch = await self.create_dm()
return ch return ch
@ -205,10 +219,11 @@ class Member(discord.abc.Messageable, _BaseUser):
self.status = try_enum(Status, data['status']) self.status = try_enum(Status, data['status'])
self.activity = create_activity(data.get('game')) self.activity = create_activity(data.get('game'))
u = self._user if len(user) > 1:
u.name = user.get('username', u.name) u = self._user
u.avatar = user.get('avatar', u.avatar) u.name = user.get('username', u.name)
u.discriminator = user.get('discriminator', u.discriminator) u.avatar = user.get('avatar', u.avatar)
u.discriminator = user.get('discriminator', u.discriminator)
def _copy(self): def _copy(self):
c = copy.copy(self) c = copy.copy(self)

2
discord/state.py

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

13
discord/user.py

@ -91,6 +91,19 @@ class BaseUser(_BaseUser):
def __hash__(self): def __hash__(self):
return self.id >> 22 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 @property
def avatar_url(self): def avatar_url(self):
"""Returns a friendly URL version of the avatar the user has. """Returns a friendly URL version of the avatar the user has.

Loading…
Cancel
Save