aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Manuel Ignacio Pérez Alcolea <[email protected]>2019-12-16 01:44:38 -0300
committerGravatar Manuel Ignacio Pérez Alcolea <[email protected]>2019-12-16 01:44:38 -0300
commitd8a9261941e2cfc027f14f2f56aae0b4f55858dd (patch)
treef3fdb52dbf76defcfa65905ca54bec5b20d308ff
parentUse OAuth to be Reddit API compliant (#696) (diff)
Add FetchedUser to convert ids of absent users to `discord.User`
This `discord.ext.commands.Converter` fetches a user from the Discord API and returns a `discord.User` object. This should replace the `proxy_user` function from the moderation `utils`.
-rw-r--r--bot/converters.py22
1 files changed, 22 insertions, 0 deletions
diff --git a/bot/converters.py b/bot/converters.py
index 8d2ab7eb8..fbe9ecd90 100644
--- a/bot/converters.py
+++ b/bot/converters.py
@@ -278,3 +278,25 @@ class ISODateTime(Converter):
dt = dt.replace(tzinfo=None)
return dt
+
+
+class FetchedUser(Converter):
+ """Fetches from the Discord API and returns a `discord.User` object."""
+
+ @staticmethod
+ async def convert(ctx: Context, user_id: str) -> discord.User:
+ """Converts `user_id` to a `discord.User` object, after fetching from the Discord API."""
+ # TODO: add docstring
+ # TODO: add remaining exceptions
+ try:
+ user_id = int(user_id)
+ user = await ctx.bot.fetch_user(user_id)
+ 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
+ if e.code != 10013:
+ log.warning(f"Failed to fetch user:")
+ raise BadArgument
+
+ return user