Browse Source

[commands] Fetch user if an ID is passed and cache lookup fails.

pull/6090/head
Rapptz 4 years ago
parent
commit
0456458ad1
  1. 47
      discord/ext/commands/converter.py

47
discord/ext/commands/converter.py

@ -206,6 +206,10 @@ class UserConverter(IDConverter):
.. versionchanged:: 1.5 .. versionchanged:: 1.5
Raise :exc:`.UserNotFound` instead of generic :exc:`.BadArgument` 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): async def convert(self, ctx, argument):
match = self._get_id_match(argument) or re.match(r'<@!?([0-9]+)>$', 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: if match is not None:
user_id = int(match.group(1)) user_id = int(match.group(1))
result = ctx.bot.get_user(user_id) or _utils_get(ctx.message.mentions, id=user_id) result = ctx.bot.get_user(user_id) or _utils_get(ctx.message.mentions, id=user_id)
else: if result is None:
arg = argument try:
result = await ctx.bot.fetch_user(user_id)
# Remove the '@' character if this is the first character from the argument except discord.HTTPException:
if arg[0] == '@': raise UserNotFound(argument) from None
# Remove first character
arg = arg[1:] return result
# check for discriminator if it exists, arg = argument
if len(arg) > 5 and arg[-5] == '#':
discrim = arg[-4:] # Remove the '@' character if this is the first character from the argument
name = arg[:-5] if arg[0] == '@':
predicate = lambda u: u.name == name and u.discriminator == discrim # Remove first character
result = discord.utils.find(predicate, state._users.values()) arg = arg[1:]
if result is not None:
return result # check for discriminator if it exists,
if len(arg) > 5 and arg[-5] == '#':
predicate = lambda u: u.name == arg discrim = arg[-4:]
name = arg[:-5]
predicate = lambda u: u.name == name and u.discriminator == discrim
result = discord.utils.find(predicate, state._users.values()) 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: if result is None:
raise UserNotFound(argument) raise UserNotFound(argument)

Loading…
Cancel
Save