From 9179fef1c40326625d38f8137d2e905765b87f7b Mon Sep 17 00:00:00 2001 From: Soheab_ <33902984+Soheab@users.noreply.github.com> Date: Mon, 27 Nov 2023 19:02:22 +0100 Subject: [PATCH 1/3] Add Client.get_user_named --- discord/client.py | 45 +++++++++++++++++++++++++++++++ discord/ext/commands/converter.py | 13 +-------- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/discord/client.py b/discord/client.py index ccafc073d..e7a355f6b 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1048,6 +1048,51 @@ class Client: """ 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`] + 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. diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py index 7255f1715..8176d323d 100644 --- a/discord/ext/commands/converter.py +++ b/discord/ext/commands/converter.py @@ -320,18 +320,7 @@ class UserConverter(IDConverter[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) From b90d383647eee74d18fd0206be6eea080352f066 Mon Sep 17 00:00:00 2001 From: Soheab_ <33902984+Soheab@users.noreply.github.com> Date: Wed, 29 Nov 2023 09:00:42 +0100 Subject: [PATCH 2/3] Fix docs Co-authored-by: lmaotrigine <57328245+lmaotrigine@users.noreply.github.com> --- discord/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/client.py b/discord/client.py index e7a355f6b..a8ea88f1e 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1073,7 +1073,7 @@ class Client: Returns -------- - Optional[:class:`User`] + Optional[:class:`~discord.User`] The user sharing a server with the bot with the associated name. If not found then ``None`` is returned. """ From 05f7dedab35e05bd8c012c6b5be6a14ab4594645 Mon Sep 17 00:00:00 2001 From: Soheab_ <33902984+Soheab@users.noreply.github.com> Date: Sun, 10 Dec 2023 22:10:17 +0100 Subject: [PATCH 3/3] Compare name over global_name --- discord/client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/discord/client.py b/discord/client.py index a8ea88f1e..07e6132ad 100644 --- a/discord/client.py +++ b/discord/client.py @@ -1089,7 +1089,7 @@ class Client: 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 + pred = lambda u: u.name == name or u.global_name == name return utils.find(pred, users)