diff options
| author | JustAnyone <[email protected]> | 2021-08-22 13:49:42 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2021-08-22 06:49:42 -0400 |
| commit | 91652e3b6017ac68f5a183ce6541e055b4b53ec7 (patch) | |
| tree | 2a05dd76e6fae0cc9da6f9304d42aad798987299 /discord | |
| parent | Typehint opus.py (diff) | |
| download | discord.py-91652e3b6017ac68f5a183ce6541e055b4b53ec7.tar.xz discord.py-91652e3b6017ac68f5a183ce6541e055b4b53ec7.zip | |
Add per-guild member avatar support
Fix #7054
Diffstat (limited to 'discord')
| -rw-r--r-- | discord/asset.py | 11 | ||||
| -rw-r--r-- | discord/member.py | 26 | ||||
| -rw-r--r-- | discord/user.py | 12 |
3 files changed, 49 insertions, 0 deletions
diff --git a/discord/asset.py b/discord/asset.py index 8aa43914..c58cfd36 100644 --- a/discord/asset.py +++ b/discord/asset.py @@ -178,6 +178,17 @@ class Asset(AssetMixin): ) @classmethod + def _from_guild_avatar(cls, state, guild_id: int, member_id: int, avatar: str) -> Asset: + animated = avatar.startswith('a_') + format = 'gif' if animated else 'png' + return cls( + state, + url=f"{cls.BASE}/guilds/{guild_id}/users/{member_id}/avatars/{avatar}.{format}?size=1024", + key=avatar, + animated=animated, + ) + + @classmethod def _from_icon(cls, state, object_id: int, icon_hash: str, path: str) -> Asset: return cls( state, diff --git a/discord/member.py b/discord/member.py index 6a5afa42..fa4df1c7 100644 --- a/discord/member.py +++ b/discord/member.py @@ -34,6 +34,7 @@ from typing import Any, Dict, List, Literal, Optional, TYPE_CHECKING, Tuple, Typ import discord.abc from . import utils +from .asset import Asset from .utils import MISSING from .user import BaseUser, User, _UserTag from .activity import create_activity, ActivityTypes @@ -263,6 +264,7 @@ class Member(discord.abc.Messageable, _UserTag): '_client_status', '_user', '_state', + '_avatar', ) if TYPE_CHECKING: @@ -293,6 +295,7 @@ class Member(discord.abc.Messageable, _UserTag): self.activities: Tuple[ActivityTypes, ...] = tuple() self.nick: Optional[str] = data.get('nick', None) self.pending: bool = data.get('pending', False) + self._avatar: Optional[str] = data.get("avatar", None) def __str__(self) -> str: return str(self._user) @@ -499,6 +502,29 @@ class Member(discord.abc.Messageable, _UserTag): return self.nick or self.name @property + def display_avatar(self) -> Asset: + """:class:`Asset`: Returns the member's display avatar. + + For regular members this is just their avatar, but + if they have a guild specific avatar then that + is returned instead. + + .. versionadded:: 2.0 + """ + return self.guild_avatar or self.avatar + + @property + def guild_avatar(self) -> Optional[Asset]: + """Optional[:class:`Asset`:] Returns an :class:`Asset` for the guild avatar + the member has if available. + + .. versionadded:: 2.0 + """ + if self._avatar is None: + return None + return Asset._from_guild_avatar(self._state, self.guild.id, self.id, self._avatar) + + @property def activity(self) -> Optional[ActivityTypes]: """Optional[Union[:class:`BaseActivity`, :class:`Spotify`]]: Returns the primary activity the user is currently doing. Could be ``None`` if no activity is being done. diff --git a/discord/user.py b/discord/user.py index 03a81fc9..83fb8fd4 100644 --- a/discord/user.py +++ b/discord/user.py @@ -238,6 +238,18 @@ 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. |