Skip to content

Commit

Permalink
Add per-guild member avatar support
Browse files Browse the repository at this point in the history
Fix #7054
  • Loading branch information
JustAnyones authored Aug 22, 2021
1 parent 9db8698 commit 91652e3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
11 changes: 11 additions & 0 deletions discord/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ def _from_avatar(cls, state, user_id: int, avatar: str) -> Asset:
animated=animated,
)

@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(
Expand Down
26 changes: 26 additions & 0 deletions discord/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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
Expand Down Expand Up @@ -263,6 +264,7 @@ class Member(discord.abc.Messageable, _UserTag):
'_client_status',
'_user',
'_state',
'_avatar',
)

if TYPE_CHECKING:
Expand Down Expand Up @@ -293,6 +295,7 @@ def __init__(self, *, data: GatewayMemberPayload, guild: Guild, state: Connectio
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)
Expand Down Expand Up @@ -498,6 +501,29 @@ def display_name(self) -> str:
"""
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
Expand Down
12 changes: 12 additions & 0 deletions discord/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,18 @@ def display_name(self) -> str:
"""
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.
Expand Down

0 comments on commit 91652e3

Please # to comment.