Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/fablabot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
):
Comment thread
Copilot marked this conversation as resolved.
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."""
Expand Down Expand Up @@ -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.")
Expand Down