diff --git a/discord/client.py b/discord/client.py index ccafc073d1ea..07e6132ade54 100644 --- a/discord/client.py +++ b/discord/client.py @@ -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:`~discord.User`] + 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.name == name or u.global_name == name + + return utils.find(pred, users) + def get_emoji(self, id: int, /) -> Optional[Emoji]: """Returns an emoji with the given ID. diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py index 7255f171540b..8176d323d74a 100644 --- a/discord/ext/commands/converter.py +++ b/discord/ext/commands/converter.py @@ -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)