Skip to content

Commit

Permalink
Add the ability to fetch a user by name
Browse files Browse the repository at this point in the history
dolfies committed Jan 15, 2024
1 parent 4fe997d commit 12ee711
Showing 2 changed files with 69 additions and 0 deletions.
63 changes: 63 additions & 0 deletions discord/client.py
Original file line number Diff line number Diff line change
@@ -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,
6 changes: 6 additions & 0 deletions discord/http.py
Original file line number Diff line number Diff line change
@@ -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,

0 comments on commit 12ee711

Please sign in to comment.