From bbc50c92d006398496bfee098dad20087ea86b6f Mon Sep 17 00:00:00 2001 From: Rapptz Date: Wed, 10 Jul 2019 04:32:11 -0400 Subject: [PATCH] Add upgraded Member to Message.mentions in case of no cache. --- discord/member.py | 11 +++++++++++ discord/message.py | 14 +++++++++----- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/discord/member.py b/discord/member.py index d78710666..bd65b2374 100644 --- a/discord/member.py +++ b/discord/member.py @@ -192,6 +192,17 @@ class Member(discord.abc.Messageable, _BaseUser): data['user'] = author._to_minimal_user_json() return cls(data=data, guild=message.guild, state=message._state) + @classmethod + def _try_upgrade(cls, *, data, guild, state): + # A User object with a 'member' key + try: + member_data = data.pop('member') + except KeyError: + return state.store_user(member_data) + else: + member_data['user'] = data + return cls(data=member_data, guild=guild, state=state) + @classmethod def _from_presence_update(cls, *, data, guild, state): clone = cls(data=data, guild=guild, state=state) diff --git a/discord/message.py b/discord/message.py index 3d7e5a0ba..eb6ab5c78 100644 --- a/discord/message.py +++ b/discord/message.py @@ -412,16 +412,20 @@ class Message: self.author = Member._from_message(message=self, data=member) def _handle_mentions(self, mentions): - self.mentions = [] - if self.guild is None: - self.mentions = [self._state.store_user(m) for m in mentions] + self.mentions = r = [] + guild = self.guild + state = self._state + if guild is None: + self.mentions = [state.store_user(m) for m in mentions] return for mention in filter(None, mentions): id_search = int(mention['id']) - member = self.guild.get_member(id_search) + member = guild.get_member(id_search) if member is not None: - self.mentions.append(member) + r.append(member) + else: + r.append(Member._try_upgrade(data=mention, guild=guild, state=state)) def _handle_mention_roles(self, role_mentions): self.role_mentions = []