diff options
| author | Rapptz <[email protected]> | 2020-11-23 06:22:23 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2020-11-23 06:22:44 -0500 |
| commit | 0456458ad193750db7694e3434f4d52bd87fafe9 (patch) | |
| tree | d0e447f8cca0230471856444491ea9a060104c45 | |
| parent | Sticker implementation cleanup (diff) | |
| download | discord.py-0456458ad193750db7694e3434f4d52bd87fafe9.tar.xz discord.py-0456458ad193750db7694e3434f4d52bd87fafe9.zip | |
[commands] Fetch user if an ID is passed and cache lookup fails.
| -rw-r--r-- | discord/ext/commands/converter.py | 47 |
1 files changed, 29 insertions, 18 deletions
diff --git a/discord/ext/commands/converter.py b/discord/ext/commands/converter.py index 2ff05843..85a301c6 100644 --- a/discord/ext/commands/converter.py +++ b/discord/ext/commands/converter.py @@ -206,6 +206,10 @@ class UserConverter(IDConverter): .. versionchanged:: 1.5 Raise :exc:`.UserNotFound` instead of generic :exc:`.BadArgument` + + .. versionchanged:: 1.6 + This converter now lazily fetches users from the HTTP APIs if an ID is passed + and it's not available in cache. """ async def convert(self, ctx, argument): match = self._get_id_match(argument) or re.match(r'<@!?([0-9]+)>$', argument) @@ -215,25 +219,32 @@ class UserConverter(IDConverter): if match is not None: user_id = int(match.group(1)) result = ctx.bot.get_user(user_id) or _utils_get(ctx.message.mentions, id=user_id) - else: - arg = argument - - # Remove the '@' character if this is the first character from the argument - if arg[0] == '@': - # Remove first character - arg = arg[1:] - - # check for discriminator if it exists, - if len(arg) > 5 and arg[-5] == '#': - discrim = arg[-4:] - name = arg[:-5] - predicate = lambda u: u.name == name and u.discriminator == discrim - result = discord.utils.find(predicate, state._users.values()) - if result is not None: - return result - - predicate = lambda u: u.name == arg + if result is None: + try: + result = await ctx.bot.fetch_user(user_id) + except discord.HTTPException: + raise UserNotFound(argument) from None + + return result + + arg = argument + + # Remove the '@' character if this is the first character from the argument + if arg[0] == '@': + # Remove first character + arg = arg[1:] + + # check for discriminator if it exists, + if len(arg) > 5 and arg[-5] == '#': + discrim = arg[-4:] + name = arg[:-5] + predicate = lambda u: u.name == name and u.discriminator == discrim result = discord.utils.find(predicate, state._users.values()) + if result is not None: + return result + + predicate = lambda u: u.name == arg + result = discord.utils.find(predicate, state._users.values()) if result is None: raise UserNotFound(argument) |