Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Client.get_user_named #9658

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
45 changes: 45 additions & 0 deletions discord/client.py
Expand Up @@ -1048,6 +1048,51 @@ def get_user(self, id: int, /) -> Optional[User]:
"""
return self._connection.get_user(id)

def get_user_named(self, name: str, /) -> Optional[User]:
"""Returns the first user found that matches the name provided.

The name is looked up in the following order:

- Username#Discriminator (deprecated)
- Username#0 (deprecated, only gets users that migrated from their discriminator)
- Global name
- Username

If no user is found, ``None`` is returned.

.. versionadded:: 2.4
.. deprecated:: 2.4

Looking up users via discriminator due to Discord API change.


Parameters
-----------
name: :class:`str`
The name of the user to lookup.

Returns
--------
Optional[:class:`User`]
Soheab marked this conversation as resolved.
Show resolved Hide resolved
The user sharing a server with the bot with the associated name. If not found
then ``None`` is returned.
"""

users = self.users

username, _, discriminator = name.rpartition('#')

# If # isn't found then "discriminator" actually has the username
if not username:
discriminator, username = username, discriminator

if discriminator == '0' or (len(discriminator) == 4 and discriminator.isdigit()):
pred = lambda u: u.name == username and u.discriminator == discriminator
else:
pred = lambda u: u.global_name == name or u.name == name

return utils.find(pred, users)

def get_emoji(self, id: int, /) -> Optional[Emoji]:
"""Returns an emoji with the given ID.

Expand Down
13 changes: 1 addition & 12 deletions discord/ext/commands/converter.py
Expand Up @@ -320,18 +320,7 @@ async def convert(self, ctx: Context[BotT], argument: str) -> discord.User:

return result # type: ignore

username, _, discriminator = argument.rpartition('#')

# If # isn't found then "discriminator" actually has the username
if not username:
discriminator, username = username, discriminator

if discriminator == '0' or (len(discriminator) == 4 and discriminator.isdigit()):
predicate = lambda u: u.name == username and u.discriminator == discriminator
else:
predicate = lambda u: u.name == argument or u.global_name == argument

result = discord.utils.find(predicate, state._users.values())
result = ctx.bot.get_user_named(argument)
if result is None:
raise UserNotFound(argument)

Expand Down