diff options
author | 2021-05-29 21:57:41 -0400 | |
---|---|---|
committer | 2021-05-29 21:57:41 -0400 | |
commit | 7224b61e50f1caaaad96c09a2532e46a600988b6 (patch) | |
tree | b1b1cb4f8eeb8a7b2a252e48a56b17667b077fb2 | |
parent | Merge branch 'main' of https://github.com/python-discord/bot into swfarnswort... (diff) |
Re-introduced static method for role change exception handling.
A function that did the same thing previously existed in `_cooldown.py`.
-rw-r--r-- | bot/exts/help_channels/_cog.py | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py index 6cd31df38..49640dda7 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -94,6 +94,25 @@ class HelpChannels(commands.Cog): self.scheduler.cancel_all() + @staticmethod + async def _handle_role_change(member: discord.Member, coro: t.Coroutine) -> None: + """ + Change `member`'s cooldown role via awaiting `coro` and handle errors. + + `coro` is intended to be `discord.Member.add_roles` or `discord.Member.remove_roles`. + """ + try: + await coro + except discord.NotFound: + log.debug(f"Failed to change role for {member} ({member.id}): member not found") + except discord.Forbidden: + log.debug( + f"Forbidden to change role for {member} ({member.id}); " + f"possibly due to role hierarchy" + ) + except discord.HTTPException as e: + log.error(f"Failed to change role for {member} ({member.id}): {e.status} {e.code}") + @lock.lock_arg(NAMESPACE, "message", attrgetter("channel.id")) @lock.lock_arg(NAMESPACE, "message", attrgetter("author.id")) @lock.lock_arg(f"{NAMESPACE}.unclaim", "message", attrgetter("author.id"), wait=True) @@ -107,7 +126,7 @@ class HelpChannels(commands.Cog): log.info(f"Channel #{message.channel} was claimed by `{message.author.id}`.") await self.move_to_in_use(message.channel) cooldown_role = self.bot.get_guild(constants.Guild.id).get_role(constants.Roles.help_cooldown) - await message.author.add_roles(cooldown_role) + await self._handle_role_change(message.author, message.author.add_roles(cooldown_role)) await _message.pin(message) @@ -413,7 +432,7 @@ class HelpChannels(commands.Cog): log.info(f"{claimant_id} left the guild during their help session; the cooldown role won't be removed") else: cooldown_role = self.bot.get_guild(constants.Guild.id).get_role(constants.Roles.help_cooldown) - await claimant.remove_roles(cooldown_role) + await self._handle_role_change(claimant, claimant.remove_roles(cooldown_role)) await _message.unpin(channel) await _stats.report_complete_session(channel.id, closed_on) |