From b21c9d10b83b5c7b569e98e39ce0dfc1173d0c60 Mon Sep 17 00:00:00 2001 From: dolfies Date: Fri, 21 Apr 2023 23:26:22 -0400 Subject: [PATCH] Move various functionality from User to BaseUser, type it in Member --- discord/member.py | 11 ++++ discord/user.py | 138 +++++++++++++++++++++++----------------------- 2 files changed, 80 insertions(+), 69 deletions(-) diff --git a/discord/member.py b/discord/member.py index 120554a42..070fd2002 100644 --- a/discord/member.py +++ b/discord/member.py @@ -77,6 +77,9 @@ if TYPE_CHECKING: GuildVoiceState as GuildVoiceStatePayload, VoiceState as VoiceStatePayload, ) + from .user import Note + from .relationship import Relationship + from .calls import PrivateCall VocalGuildChannel = Union[VoiceChannel, StageChannel] ConnectableChannel = Union[VocalGuildChannel, DMChannel, GroupChannel] @@ -290,8 +293,16 @@ class Member(discord.abc.Messageable, discord.abc.Connectable, _UserTag): default_avatar: Asset avatar: Optional[Asset] avatar_decoration: Optional[Asset] + note: Note + relationship: Optional[Relationship] + is_friend: Callable[[], bool] + is_blocked: Callable[[], bool] dm_channel: Optional[DMChannel] + call: Optional[PrivateCall] create_dm: Callable[[], Awaitable[DMChannel]] + block: Callable[[], Awaitable[None]] + unblock: Callable[[], Awaitable[None]] + remove_friend: Callable[[], Awaitable[None]] mutual_guilds: List[Guild] public_flags: PublicUserFlags banner: Optional[Asset] diff --git a/discord/user.py b/discord/user.py index 85ca110ad..cbfd342fe 100644 --- a/discord/user.py +++ b/discord/user.py @@ -506,6 +506,74 @@ class BaseUser(_UserTag): return any(user.id == self.id for user in message.mentions) + @property + def relationship(self) -> Optional[Relationship]: + """Optional[:class:`Relationship`]: Returns the :class:`Relationship` with this user if applicable, ``None`` otherwise.""" + return self._state._relationships.get(self.id) + + def is_friend(self) -> bool: + """:class:`bool`: Checks if the user is your friend.""" + r = self.relationship + if r is None: + return False + return r.type is RelationshipType.friend + + def is_blocked(self) -> bool: + """:class:`bool`: Checks if the user is blocked.""" + r = self.relationship + if r is None: + return False + return r.type is RelationshipType.blocked + + async def profile( + self, + *, + with_mutual_guilds: bool = True, + with_mutual_friends_count: bool = False, + with_mutual_friends: bool = True, + ) -> UserProfile: + """|coro| + + A shorthand method to retrieve a :class:`UserProfile` for the user. + + Parameters + ------------ + with_mutual_guilds: :class:`bool` + Whether to fetch mutual guilds. + This fills in :attr:`UserProfile.mutual_guilds`. + + .. versionadded:: 2.0 + with_mutual_friends_count: :class:`bool` + Whether to fetch the number of mutual friends. + This fills in :attr:`UserProfile.mutual_friends_count`. + + .. versionadded:: 2.0 + with_mutual_friends: :class:`bool` + Whether to fetch mutual friends. + This fills in :attr:`UserProfile.mutual_friends` and :attr:`UserProfile.mutual_friends_count`, + but requires an extra API call. + + .. versionadded:: 2.0 + + Raises + ------- + Forbidden + Not allowed to fetch this profile. + HTTPException + Fetching the profile failed. + + Returns + -------- + :class:`UserProfile` + The profile of the user. + """ + return await self._state.client.fetch_user_profile( + self.id, + with_mutual_guilds=with_mutual_guilds, + with_mutual_friends_count=with_mutual_friends_count, + with_mutual_friends=with_mutual_friends, + ) + class ClientUser(BaseUser): """Represents your Discord user. @@ -920,11 +988,6 @@ class User(BaseUser, discord.abc.Connectable, discord.abc.Messageable): """Optional[:class:`PrivateCall`]: Returns the call associated with this user if it exists.""" return getattr(self.dm_channel, 'call', None) - @property - def relationship(self) -> Optional[Relationship]: - """Optional[:class:`Relationship`]: Returns the :class:`Relationship` with this user if applicable, ``None`` otherwise.""" - return self._state._relationships.get(self.id) - @copy_doc(discord.abc.Connectable.connect) async def connect( self, @@ -961,21 +1024,7 @@ class User(BaseUser, discord.abc.Connectable, discord.abc.Messageable): data: DMChannelPayload = await state.http.start_private_message(self.id) return state.add_dm_channel(data) - def is_friend(self) -> bool: - """:class:`bool`: Checks if the user is your friend.""" - r = self.relationship - if r is None: - return False - return r.type is RelationshipType.friend - - def is_blocked(self) -> bool: - """:class:`bool`: Checks if the user is blocked.""" - r = self.relationship - if r is None: - return False - return r.type is RelationshipType.blocked - - async def block(self) -> None: # TODO: maybe return relationship + async def block(self) -> None: """|coro| Blocks the user. @@ -1032,52 +1081,3 @@ class User(BaseUser, discord.abc.Connectable, discord.abc.Messageable): Sending the friend request failed. """ await self._state.http.send_friend_request(self.name, self.discriminator) - - async def profile( - self, - *, - with_mutual_guilds: bool = True, - with_mutual_friends_count: bool = False, - with_mutual_friends: bool = True, - ) -> UserProfile: - """|coro| - - A shorthand method to retrieve a :class:`UserProfile` for the user. - - Parameters - ------------ - with_mutual_guilds: :class:`bool` - Whether to fetch mutual guilds. - This fills in :attr:`UserProfile.mutual_guilds`. - - .. versionadded:: 2.0 - with_mutual_friends_count: :class:`bool` - Whether to fetch the number of mutual friends. - This fills in :attr:`UserProfile.mutual_friends_count`. - - .. versionadded:: 2.0 - with_mutual_friends: :class:`bool` - Whether to fetch mutual friends. - This fills in :attr:`UserProfile.mutual_friends` and :attr:`UserProfile.mutual_friends_count`, - but requires an extra API call. - - .. versionadded:: 2.0 - - Raises - ------- - Forbidden - Not allowed to fetch this profile. - HTTPException - Fetching the profile failed. - - Returns - -------- - :class:`UserProfile` - The profile of the user. - """ - return await self._state.client.fetch_user_profile( - self.id, - with_mutual_guilds=with_mutual_guilds, - with_mutual_friends_count=with_mutual_friends_count, - with_mutual_friends=with_mutual_friends, - )