diff options
| author | 2019-12-22 17:27:42 -0300 | |
|---|---|---|
| committer | 2019-12-22 17:27:42 -0300 | |
| commit | e30cedc63e06ea136947934e579824f9e3b744bf (patch) | |
| tree | 07777673d3b178a57cf568c4c2ec4452712c539f | |
| parent | Make `watchchannels` use `FetchedUser` instead of `proxy_user` (diff) | |
Catch HTTPException in fetching, only fetch if isn't User or Member
There's now a check to see if the `user` argument (possibly a
`discord.Object`) needs to be made a `User`, instead of doing so
directly, to avoid unnecessary requests to the Discord API. Besides
that, a possible HTTPException is catched if it the fetch fails,
cancelling the message to be send to the user (which would make the
following calls fail later on for not being of the proper type.)
| -rw-r--r-- | bot/cogs/moderation/scheduler.py | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/bot/cogs/moderation/scheduler.py b/bot/cogs/moderation/scheduler.py index 01e4b1fe7..4b3d553c2 100644 --- a/bot/cogs/moderation/scheduler.py +++ b/bot/cogs/moderation/scheduler.py @@ -106,16 +106,20 @@ class InfractionScheduler(Scheduler): # DM the user about the infraction if it's not a shadow/hidden infraction. if not infraction["hidden"]: - # Sometimes user is a discord.Object; make it a proper user. - user = await self.bot.fetch_user(user.id) + dm_result = f"{constants.Emojis.failmail} " + dm_log_text = "\nDM: **Failed**" - # Accordingly display whether the user was successfully notified via DM. - if await utils.notify_infraction(user, infr_type, expiry, reason, icon): - dm_result = ":incoming_envelope: " - dm_log_text = "\nDM: Sent" + # Sometimes user is a discord.Object; make it a proper user. + try: + if not isinstance(user, (discord.Member, discord.User)): + user = await self.bot.fetch_user(user.id) + except discord.HTTPException as e: + log.error(f"Failed to fetch user `{user.id}`: status {e.status}") else: - dm_result = f"{constants.Emojis.failmail} " - dm_log_text = "\nDM: **Failed**" + # Accordingly display whether the user was successfully notified via DM. + if await utils.notify_infraction(user, infr_type, expiry, reason, icon): + dm_result = ":incoming_envelope: " + dm_log_text = "\nDM: Sent" if infraction["actor"] == self.bot.user.id: log.trace( |