diff --git a/discord/guild.py b/discord/guild.py index adb3e22e4..0f37a214a 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -226,13 +226,6 @@ class Guild(Hashable): self.splash = guild.get('splash') for mdata in guild.get('members', []): - roles = [self.default_role] - for role_id in mdata['roles']: - role = utils.find(lambda r: r.id == role_id, self.roles) - if role is not None: - roles.append(role) - - mdata['roles'] = roles member = Member(data=mdata, guild=self, state=self._state) self._add_member(member) diff --git a/discord/member.py b/discord/member.py index 3d9928dc2..467bdbbeb 100644 --- a/discord/member.py +++ b/discord/member.py @@ -145,12 +145,12 @@ class Member(discord.abc.Messageable): def __init__(self, *, data, guild, state): self._state = state self._user = state.store_user(data['user']) + self.guild = guild self.joined_at = utils.parse_time(data.get('joined_at')) - self.roles = data.get('roles', []) + self._update_roles(data) self.status = Status.offline game = data.get('game', {}) self.game = Game(**game) if game else None - self.guild = guild self.nick = data.get('nick', None) def __str__(self): @@ -177,6 +177,22 @@ class Member(discord.abc.Messageable): def _get_guild_id(self): return None + def _update_roles(self, data): + # update the roles + self.roles = [self.guild.default_role] + for role in self.guild.roles: + if role.id in data['roles']: + self.roles.append(role) + + self.roles = [self.guild.default_role] + for roleid in map(int, data['roles']): + role = utils.find(lambda r: r.id == roleid, self.guild.roles) + if role is not None: + self.roles.append(role) + + # sort the roles by ID since they can be "randomised" + self.roles.sort(key=lambda r: r.id) + def _update(self, data, user=None): if user: self._user.name = user['username'] @@ -191,14 +207,7 @@ class Member(discord.abc.Messageable): except KeyError: pass - # update the roles - self.roles = [self.guild.default_role] - for role in self.guild.roles: - if role.id in data['roles']: - self.roles.append(role) - - # sort the roles by ID since they can be "randomised" - self.roles.sort(key=lambda r: int(r.id)) + self._update_roles(data) def _presence_update(self, data, user): self.status = try_enum(Status, data['status']) diff --git a/discord/state.py b/discord/state.py index 31f295451..e839d5ab0 100644 --- a/discord/state.py +++ b/discord/state.py @@ -330,7 +330,7 @@ class ConnectionState: # skip these useless cases. return - member = self._make_member(guild, data) + member = Member(guild=guild, data=data, state=self) guild._add_member(member) old_member = copy.copy(member) @@ -402,19 +402,9 @@ class ConnectionState: else: self.dispatch('group_remove', channel, user) - def _make_member(self, guild, data): - roles = [guild.default_role] - for roleid in data.get('roles', []): - role = utils.get(guild.roles, id=roleid) - if role is not None: - roles.append(role) - - data['roles'] = sorted(roles, key=lambda r: r.id) - return Member(guild=guild, data=data, state=self) - def parse_guild_member_add(self, data): guild = self._get_guild(int(data['guild_id'])) - member = self._make_member(guild, data) + member = Member(guild=guild, data=data, state=self) guild._add_member(member) guild._member_count += 1 self.dispatch('member_join', member) @@ -605,7 +595,7 @@ class ConnectionState: guild = self._get_guild(int(data['guild_id'])) members = data.get('members', []) for member in members: - m = self._make_member(guild, member) + m = Member(guild=guild, data=member, state=self) existing = guild.get_member(m.id) if existing is None or existing.joined_at is None: guild._add_member(m)