diff --git a/src/fablabot/main.py b/src/fablabot/main.py index 8ebd352..e091b16 100644 --- a/src/fablabot/main.py +++ b/src/fablabot/main.py @@ -31,6 +31,31 @@ with open(DISCORD_TOKEN_FILE) as f: DISCORD_TOKEN = f.read().strip() +class DiscordGateway503Filter(logging.Filter): + """Filter Discord gateway 503 warnings into a cleaner message.""" + + def filter(self, record: logging.LogRecord) -> bool: + if not record.exc_info: + return True + + exception = record.exc_info[1] + + from aiohttp import WSServerHandshakeError + + if ( + isinstance(exception, WSServerHandshakeError) + and getattr(exception, "status", None) == 503 + ): + retry_message = record.getMessage() + + logger.warning( + "Discord Gateway indisponible (503). %s", + retry_message, + ) + + return False + + return True class Fablabot(commands.Bot): """Discord bot implementation for the DeVinci Fablab server.""" @@ -99,6 +124,9 @@ async def on_command_error(self, ctx: commands.Context[Any], exception: Exceptio def main() -> None: """Run the bot using the token from the environment.""" + discord_logger = logging.getLogger("discord.client") + discord_logger.addFilter(DiscordGateway503Filter()) + logger.info("Starting FablaBot.") bot = Fablabot() logger.info("Running bot.")