Browse Source

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

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

13
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,7 +219,14 @@ 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:
try:
result = await ctx.bot.fetch_user(user_id)
except discord.HTTPException:
raise UserNotFound(argument) from None
return result
arg = argument arg = argument
# Remove the '@' character if this is the first character from the argument # Remove the '@' character if this is the first character from the argument

Loading…
Cancel
Save