diff options
-rw-r--r-- | bot/bot.py | 16 | ||||
-rw-r--r-- | bot/exts/backend/error_handler.py | 8 |
2 files changed, 23 insertions, 1 deletions
diff --git a/bot/bot.py b/bot/bot.py index 92ecbf130..84caf1873 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -1,7 +1,11 @@ import asyncio +import contextlib +from sys import exception import aiohttp +from discord.errors import Forbidden from pydis_core import BotBase +from pydis_core.utils.error_handling import handle_forbidden_from_block from sentry_sdk import push_scope from bot import constants, exts @@ -48,6 +52,18 @@ class Bot(BotBase): async def on_error(self, event: str, *args, **kwargs) -> None: """Log errors raised in event listeners rather than printing them to stderr.""" + e_val = exception() + + if isinstance(e_val, Forbidden): + message = args[0] if event == "on_message" else args[1] if event == "on_message_edit" else None + + with contextlib.suppress(Forbidden): + # Attempt to handle the error. This reraises the error if's not due to a block, + # in which case the error is suppressed and handled normally. Otherwise, it was + # handled so return. + await handle_forbidden_from_block(e_val, message) + return + self.stats.incr(f"errors.event.{event}") with push_scope() as scope: diff --git a/bot/exts/backend/error_handler.py b/bot/exts/backend/error_handler.py index c1e3eb70e..faa39db5d 100644 --- a/bot/exts/backend/error_handler.py +++ b/bot/exts/backend/error_handler.py @@ -1,9 +1,10 @@ import copy import difflib -from discord import Embed, Member +from discord import Embed, Forbidden, Member from discord.ext.commands import ChannelNotFound, Cog, Context, TextChannelConverter, VoiceChannelConverter, errors from pydis_core.site_api import ResponseCodeError +from pydis_core.utils.error_handling import handle_forbidden_from_block from sentry_sdk import push_scope from bot.bot import Bot @@ -98,6 +99,11 @@ class ErrorHandler(Cog): await ctx.send(f"{e.original} Please wait for it to finish and try again later.") elif isinstance(e.original, InvalidInfractedUserError): await ctx.send(f"Cannot infract that user. {e.original.reason}") + elif isinstance(e.original, Forbidden): + try: + await handle_forbidden_from_block(e.original, ctx.message) + except Forbidden: + await self.handle_unexpected_error(ctx, e.original) else: await self.handle_unexpected_error(ctx, e.original) elif isinstance(e, errors.ConversionError): |