Browse Source

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.
pull/7472/head
Rapptz 4 years ago
parent
commit
848d752388
  1. 4
      discord/member.py
  2. 35
      discord/user.py

4
discord/member.py

@ -275,7 +275,7 @@ class Member(discord.abc.Messageable, _UserTag):
system: bool system: bool
created_at: datetime.datetime created_at: datetime.datetime
default_avatar: Asset default_avatar: Asset
avatar: Asset avatar: Optional[Asset]
dm_channel: Optional[DMChannel] dm_channel: Optional[DMChannel]
create_dm = User.create_dm create_dm = User.create_dm
mutual_guilds: List[Guild] mutual_guilds: List[Guild]
@ -513,7 +513,7 @@ class Member(discord.abc.Messageable, _UserTag):
.. versionadded:: 2.0 .. versionadded:: 2.0
""" """
return self.guild_avatar or self.avatar return self.guild_avatar or self._user.avatar or self._user.default_avatar
@property @property
def guild_avatar(self) -> Optional[Asset]: def guild_avatar(self) -> Optional[Asset]:

35
discord/user.py

@ -147,22 +147,31 @@ class BaseUser(_UserTag):
return PublicUserFlags._from_value(self._public_flags) return PublicUserFlags._from_value(self._public_flags)
@property @property
def avatar(self) -> Asset: def avatar(self) -> Optional[Asset]:
""":class:`Asset`: Returns an :class:`Asset` for the avatar the user has. """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 If the user does not have a traditional avatar, ``None`` is returned.
the default avatar is returned instead. If you want the avatar that a user has displayed, consider :attr:`display_avatar`.
""" """
if self._avatar is None: if self._avatar is not None:
return Asset._from_default_avatar(self._state, int(self.discriminator) % len(DefaultAvatar))
else:
return Asset._from_avatar(self._state, self.id, self._avatar) return Asset._from_avatar(self._state, self.id, self._avatar)
return None
@property @property
def default_avatar(self) -> Asset: def default_avatar(self) -> Asset:
""":class:`Asset`: Returns the default avatar for a given user. This is calculated by the user's discriminator.""" """: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)) 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 @property
def banner(self) -> Optional[Asset]: def banner(self) -> Optional[Asset]:
"""Optional[:class:`Asset`]: Returns the user's banner asset, if available. """Optional[:class:`Asset`]: Returns the user's banner asset, if available.
@ -248,18 +257,6 @@ class BaseUser(_UserTag):
""" """
return self.name 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: def mentioned_in(self, message: Message) -> bool:
"""Checks if the user is mentioned in the specified message. """Checks if the user is mentioned in the specified message.

Loading…
Cancel
Save