From 12ee7110b1647ce8bbc34c10ab3a349b61b66d8a Mon Sep 17 00:00:00 2001 From: dolfies Date: Sun, 14 Jan 2024 22:01:18 -0500 Subject: [PATCH] Add the ability to fetch a user by name --- discord/client.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++ discord/http.py | 6 +++++ 2 files changed, 69 insertions(+) diff --git a/discord/client.py b/discord/client.py index 4a54ee2c0dd8..18d0e4471ad4 100644 --- a/discord/client.py +++ b/discord/client.py @@ -2281,6 +2281,69 @@ async def fetch_user(self, user_id: int, /) -> User: data = await self.http.get_user(user_id) return User(state=self._connection, data=data) + @overload + async def fetch_user_named(self, user: str, /) -> User: + ... + + @overload + async def fetch_user_named(self, username: str, discriminator: str, /) -> User: + ... + + async def fetch_user_named(self, *args: str) -> User: + """|coro| + + Retrieves a :class:`discord.User` based on their name or legacy username. + You do not have to share any guilds with the user to get this information, + however you must be able to add them as a friend. + + This function can be used in multiple ways. + + .. versionadded:: 2.1 + + .. code-block:: python + + # Passing a username + await client.fetch_user_named('jake') + + # Passing a legacy user: + await client.fetch_user_named('Jake#0001') + + # Passing a legacy username and discriminator: + await client.fetch_user_named('Jake', '0001') + + Parameters + ----------- + user: :class:`str` + The user to send the friend request to. + username: :class:`str` + The username of the user to send the friend request to. + discriminator: :class:`str` + The discriminator of the user to send the friend request to. + + Raises + ------- + Forbidden + Not allowed to send a friend request to this user. + HTTPException + Fetching the user failed. + TypeError + More than 2 parameters or less than 1 parameter was passed. + + Returns + -------- + :class:`discord.User` + The user you requested. + """ + if len(args) == 1: + username, _, discrim = args[0].partition('#') + elif len(args) == 2: + username, discrim = args + else: + raise TypeError(f'fetch_user_named() takes 1 or 2 arguments but {len(args)} were given') + + data = await self.http.get_user_named(username, discrim) + return User(state=self._connection, data=data) + async def fetch_user_profile( self, user_id: int, diff --git a/discord/http.py b/discord/http.py index a03ca96b1cee..06f251df89e8 100644 --- a/discord/http.py +++ b/discord/http.py @@ -4341,6 +4341,12 @@ async def get_gateway(self, *, encoding: str = 'json', zlib: bool = True) -> str def get_user(self, user_id: Snowflake) -> Response[user.APIUser]: return self.request(Route('GET', '/users/{user_id}', user_id=user_id)) + def get_user_named(self, username: str, dicriminator: Optional[str] = None) -> Response[user.APIUser]: + params = {} + if dicriminator: + params['discriminator'] = dicriminator + return self.request(Route('GET', '/users/username/{username}', username=username), params=params) + def get_user_profile( self, user_id: Snowflake,