Browse Source

Implement new profile badges

pull/10109/head
dolfies 2 years ago
parent
commit
32bf5d0b92
  1. 78
      discord/profile.py
  2. 9
      discord/types/profile.py
  3. 5
      docs/api.rst

78
discord/profile.py

@ -28,7 +28,7 @@ from typing import TYPE_CHECKING, List, Optional
from . import utils from . import utils
from .application import ApplicationInstallParams from .application import ApplicationInstallParams
from .asset import Asset from .asset import Asset, AssetMixin
from .connections import PartialConnection from .connections import PartialConnection
from .enums import PremiumType, try_enum from .enums import PremiumType, try_enum
from .flags import ApplicationFlags from .flags import ApplicationFlags
@ -44,6 +44,7 @@ if TYPE_CHECKING:
from .types.profile import ( from .types.profile import (
Profile as ProfilePayload, Profile as ProfilePayload,
ProfileApplication as ProfileApplicationPayload, ProfileApplication as ProfileApplicationPayload,
ProfileBadge as ProfileBadgePayload,
MutualGuild as MutualGuildPayload, MutualGuild as MutualGuildPayload,
) )
from .types.user import PartialUser as PartialUserPayload from .types.user import PartialUser as PartialUserPayload
@ -95,6 +96,9 @@ class Profile:
self.premium_guild_since: Optional[datetime] = utils.parse_time(data['premium_guild_since']) self.premium_guild_since: Optional[datetime] = utils.parse_time(data['premium_guild_since'])
self.connections: List[PartialConnection] = [PartialConnection(d) for d in data['connected_accounts']] self.connections: List[PartialConnection] = [PartialConnection(d) for d in data['connected_accounts']]
self.badges: List[ProfileBadge] = [
ProfileBadge(state=state, data=d) for d in data.get('badges', []) + data.get('guild_badges', [])
]
self.mutual_guilds: Optional[List[MutualGuild]] = ( self.mutual_guilds: Optional[List[MutualGuild]] = (
[MutualGuild(state=state, data=d) for d in data['mutual_guilds']] if 'mutual_guilds' in data else None [MutualGuild(state=state, data=d) for d in data['mutual_guilds']] if 'mutual_guilds' in data else None
) )
@ -253,6 +257,68 @@ class MutualGuild(Hashable):
return self._state._get_or_create_unavailable_guild(self.id) return self._state._get_or_create_unavailable_guild(self.id)
class ProfileBadge(AssetMixin, Hashable):
"""Represents a Discord profile badge.
.. container:: operations
.. describe:: x == y
Checks if two badges are equal.
.. describe:: x != y
Checks if two badges are not equal.
.. describe:: hash(x)
Returns the badge's hash.
.. describe:: str(x)
Returns the badge's description.
.. versionadded:: 2.1
Attributes
------------
id: :class:`str`
The badge's ID.
description: :class:`str`
The badge's description.
link: Optional[:class:`str`]
The link associated with the badge, if any.
"""
__slots__ = ('id', 'description', 'link', '_icon', '_state')
def __init__(self, *, state: ConnectionState, data: ProfileBadgePayload) -> None:
self._state = state
self.id: str = data['id']
self.description: str = data.get('description', '')
self.link: Optional[str] = data.get('link')
self._icon: str = data['icon']
def __repr__(self) -> str:
return f'<ProfileBadge id={self.id!r} description={self.description!r}>'
def __hash__(self) -> int:
return hash(self.id)
def __str__(self) -> str:
return self.description
@property
def animated(self) -> bool:
""":class:`bool`: Indicates if the badge is animated. Here for compatibility purposes."""
return False
@property
def url(self) -> str:
""":class:`str`: Returns the URL of the badge icon."""
return f'{Asset.BASE}/badge-icons/{self._icon}.png'
class UserProfile(Profile, User): class UserProfile(Profile, User):
"""Represents a Discord user's profile. """Represents a Discord user's profile.
@ -293,6 +359,10 @@ class UserProfile(Profile, User):
An aware datetime object that specifies when a user first Nitro boosted a guild. An aware datetime object that specifies when a user first Nitro boosted a guild.
connections: Optional[List[:class:`PartialConnection`]] connections: Optional[List[:class:`PartialConnection`]]
The connected accounts that show up on the profile. The connected accounts that show up on the profile.
badges: List[:class:`ProfileBadge`]
A list of badge icons that the user has.
.. versionadded:: 2.1
mutual_guilds: Optional[List[:class:`MutualGuild`]] mutual_guilds: Optional[List[:class:`MutualGuild`]]
A list of guilds that you share with the user. A list of guilds that you share with the user.
``None`` if you didn't fetch mutual guilds. ``None`` if you didn't fetch mutual guilds.
@ -307,6 +377,7 @@ class UserProfile(Profile, User):
'premium_since', 'premium_since',
'premium_guild_since', 'premium_guild_since',
'connections', 'connections',
'badges',
'mutual_guilds', 'mutual_guilds',
'mutual_friends', 'mutual_friends',
'_mutual_friends_count', '_mutual_friends_count',
@ -380,6 +451,10 @@ class MemberProfile(Profile, Member):
An aware datetime object that specifies when a user first Nitro boosted a guild. An aware datetime object that specifies when a user first Nitro boosted a guild.
connections: Optional[List[:class:`PartialConnection`]] connections: Optional[List[:class:`PartialConnection`]]
The connected accounts that show up on the profile. The connected accounts that show up on the profile.
badges: List[:class:`ProfileBadge`]
A list of badge icons that the user has.
.. versionadded:: 2.1
mutual_guilds: Optional[List[:class:`MutualGuild`]] mutual_guilds: Optional[List[:class:`MutualGuild`]]
A list of guilds that you share with the user. A list of guilds that you share with the user.
``None`` if you didn't fetch mutuals. ``None`` if you didn't fetch mutuals.
@ -395,6 +470,7 @@ class MemberProfile(Profile, Member):
'premium_since', 'premium_since',
'premium_guild_since', 'premium_guild_since',
'connections', 'connections',
'badges',
'mutual_guilds', 'mutual_guilds',
'mutual_friends', 'mutual_friends',
'_mutual_friends_count', '_mutual_friends_count',

9
discord/types/profile.py

@ -58,11 +58,20 @@ class ProfileApplication(TypedDict):
install_params: NotRequired[ApplicationInstallParams] install_params: NotRequired[ApplicationInstallParams]
class ProfileBadge(TypedDict):
id: str
description: str
icon: str
link: NotRequired[str]
class Profile(TypedDict): class Profile(TypedDict):
user: ProfileUser user: ProfileUser
user_profile: Optional[ProfileMetadata] user_profile: Optional[ProfileMetadata]
badges: List[ProfileBadge]
guild_member: NotRequired[ProfileMember] guild_member: NotRequired[ProfileMember]
guild_member_profile: NotRequired[Optional[ProfileMetadata]] guild_member_profile: NotRequired[Optional[ProfileMetadata]]
guild_badges: List[ProfileBadge]
mutual_guilds: NotRequired[List[MutualGuild]] mutual_guilds: NotRequired[List[MutualGuild]]
mutual_friends_count: NotRequired[int] mutual_friends_count: NotRequired[int]
connected_accounts: List[PartialConnection] connected_accounts: List[PartialConnection]

5
docs/api.rst

@ -6518,6 +6518,11 @@ User
:members: :members:
:inherited-members: :inherited-members:
.. attributetable:: ProfileBadge
.. autoclass:: ProfileBadge()
:members:
.. attributetable:: Note .. attributetable:: Note
.. autoclass:: Note() .. autoclass:: Note()

Loading…
Cancel
Save