Browse Source

Hoist webhook detection outside of store_user helper

pull/10109/head
Rapptz 2 years ago
committed by dolfies
parent
commit
1424412e42
  1. 2
      discord/message.py
  2. 5
      discord/state.py
  3. 4
      discord/webhook/async_.py

2
discord/message.py

@ -1772,7 +1772,7 @@ class Message(PartialMessage, Hashable):
self.nonce = value
def _handle_author(self, author: UserPayload) -> None:
self.author = self._state.store_user(author)
self.author = self._state.store_user(author, cache=self.webhook_id is None)
if isinstance(self.guild, Guild):
found = self.guild.get_member(self.author.id)
if found is not None:

5
discord/state.py

@ -748,15 +748,14 @@ class ConnectionState:
if len(self._interactions) > 15:
self._interactions.popitem(last=False)
def store_user(self, data: Union[UserPayload, PartialUserPayload]) -> User:
def store_user(self, data: Union[UserPayload, PartialUserPayload], *, cache: bool = True) -> User:
# this way is 300% faster than `dict.setdefault`.
user_id = int(data['id'])
try:
return self._users[user_id]
except KeyError:
user = User(state=self, data=data)
# TODO: with the removal of discrims this becomes a bit annoying
if user.discriminator != '0000':
if cache:
self._users[user_id] = user
return user

4
discord/webhook/async_.py

@ -511,9 +511,9 @@ class _WebhookState:
return self._parent._get_guild(guild_id)
return None
def store_user(self, data: Union[UserPayload, PartialUserPayload]) -> BaseUser:
def store_user(self, data: Union[UserPayload, PartialUserPayload], *, cache: bool = True) -> BaseUser:
if self._parent is not None:
return self._parent.store_user(data)
return self._parent.store_user(data, cache=cache)
# state parameter is artificial
return BaseUser(state=self, data=data) # type: ignore

Loading…
Cancel
Save