From 848d752388f9878cc4a17148b87ae435d220640d Mon Sep 17 00:00:00 2001 From: Rapptz Date: Wed, 25 Aug 2021 01:42:29 -0400 Subject: [PATCH] Change User.avatar to be Optional[Asset] instead of Asset This change was needed to allow users to more easily check if an uploaded avatar was set using `if user.avatar:` rather than the admittedly clunky `if user.avatar != user.default_avatar. The old behaviour with a fallback is still useful for actual display purposes, so it has been moved over to the new `User.display_avatar` attribute. This also has symmetry with the newly added `Member.display_avatar` attribute. --- discord/member.py | 4 ++-- discord/user.py | 35 ++++++++++++++++------------------- 2 files changed, 18 insertions(+), 21 deletions(-) diff --git a/discord/member.py b/discord/member.py index 1ff682c4a..058b39b21 100644 --- a/discord/member.py +++ b/discord/member.py @@ -275,7 +275,7 @@ class Member(discord.abc.Messageable, _UserTag): system: bool created_at: datetime.datetime default_avatar: Asset - avatar: Asset + avatar: Optional[Asset] dm_channel: Optional[DMChannel] create_dm = User.create_dm mutual_guilds: List[Guild] @@ -513,7 +513,7 @@ class Member(discord.abc.Messageable, _UserTag): .. versionadded:: 2.0 """ - return self.guild_avatar or self.avatar + return self.guild_avatar or self._user.avatar or self._user.default_avatar @property def guild_avatar(self) -> Optional[Asset]: diff --git a/discord/user.py b/discord/user.py index 7064c721d..57c032ac5 100644 --- a/discord/user.py +++ b/discord/user.py @@ -147,22 +147,31 @@ class BaseUser(_UserTag): return PublicUserFlags._from_value(self._public_flags) @property - def avatar(self) -> Asset: - """:class:`Asset`: Returns an :class:`Asset` for the avatar the user has. + def avatar(self) -> Optional[Asset]: + """Optional[:class:`Asset`]: Returns an :class:`Asset` for the avatar the user has. - If the user does not have a traditional avatar, an asset for - the default avatar is returned instead. + If the user does not have a traditional avatar, ``None`` is returned. + If you want the avatar that a user has displayed, consider :attr:`display_avatar`. """ - if self._avatar is None: - return Asset._from_default_avatar(self._state, int(self.discriminator) % len(DefaultAvatar)) - else: + if self._avatar is not None: return Asset._from_avatar(self._state, self.id, self._avatar) + return None @property def default_avatar(self) -> Asset: """:class:`Asset`: Returns the default avatar for a given user. This is calculated by the user's discriminator.""" return Asset._from_default_avatar(self._state, int(self.discriminator) % len(DefaultAvatar)) + @property + def display_avatar(self) -> Asset: + """:class:`Asset`: Returns the user's display avatar. + + For regular users this is just their default avatar or uploaded avatar. + + .. versionadded:: 2.0 + """ + return self.avatar or self.default_avatar + @property def banner(self) -> Optional[Asset]: """Optional[:class:`Asset`]: Returns the user's banner asset, if available. @@ -248,18 +257,6 @@ class BaseUser(_UserTag): """ return self.name - @property - def display_avatar(self) -> Asset: - """:class:`Asset`: Returns the user's display avatar. - - For regular users this is just their avatar, but - if they have a guild specific avatar then that - is returned instead. - - .. versionadded:: 2.0 - """ - return self.avatar - def mentioned_in(self, message: Message) -> bool: """Checks if the user is mentioned in the specified message.