diff options
Diffstat (limited to 'bot/converters.py')
| -rw-r--r-- | bot/converters.py | 47 |
1 files changed, 22 insertions, 25 deletions
diff --git a/bot/converters.py b/bot/converters.py index b33229cc7..28bf58cf4 100644 --- a/bot/converters.py +++ b/bot/converters.py @@ -280,36 +280,34 @@ class ISODateTime(Converter): return dt -class FetchedUser(Converter): +def proxy_user(user_id: str) -> discord.Object: """ - Fetches from the Discord API and returns a `discord.User` or `discord.Object` object, given an ID. + Create a proxy user object from the given id. - If the fetching is successful, a `discord.User` object is returned. If it fails and - the error doesn't imply the user doesn't exist, then a `discord.Object` is returned - via the `user_proxy` function. + Used when a Member or User object cannot be resolved. """ + log.trace(f"Attempting to create a proxy user for the user id {user_id}.") - # XXX: `proxy_user` shouldn't be here as a helper. - # Should wait for PR #651 to import from bot.utils.whatever, maybe? - @staticmethod - def proxy_user(user_id: str) -> discord.Object: - """ - Create a proxy user object from the given id. + try: + user_id = int(user_id) + except ValueError: + raise BadArgument - Used when a Member or User object cannot be resolved. - """ - log.trace(f"Attempting to create a proxy user for the user id {user_id}.") + user = discord.Object(user_id) + user.mention = user.id + user.avatar_url_as = lambda static_format: None - try: - user_id = int(user_id) - except ValueError: - raise BadArgument + return user - user = discord.Object(user_id) - user.mention = user.id - user.avatar_url_as = lambda static_format: None - return user +class FetchedUser(Converter): + """ + Fetches from the Discord API and returns a `discord.User` or `discord.Object` object, given an ID. + + If the fetching is successful, a `discord.User` object is returned. If it fails and + the error doesn't imply the user doesn't exist, then a `discord.Object` is returned + via the `user_proxy` function. + """ @staticmethod async def convert(ctx: Context, user_id: str) -> t.Union[discord.User, discord.Object]: @@ -320,11 +318,10 @@ class FetchedUser(Converter): except ValueError: raise BadArgument(f"The provided argument can't be turned into integer: `{user_id}`") except discord.HTTPException as e: - # If the Discord error isn't `Unknown user`, save it in the log and return a proxy + # If the Discord error isn't `Unknown user`, save it in the log and return a proxy instead if e.code != 10013: log.warning("Failed to fetch user, returning a proxy instead.") - # XXX: - return FetchedUser.proxy_user(user_id) + return proxy_user(user_id) raise BadArgument return user |