From 80918f7a42512a737f32df75e8a5276bc46a5524 Mon Sep 17 00:00:00 2001 From: "Max R. Carrara" Date: Tue, 11 Mar 2025 22:33:52 +0100 Subject: [PATCH] bot: main: register event handlers inside separate function Makes the overall program flow nicer. Signed-off-by: Max R. Carrara --- src/bot/main.py | 102 ++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/src/bot/main.py b/src/bot/main.py index 68c6802..338ec13 100644 --- a/src/bot/main.py +++ b/src/bot/main.py @@ -111,70 +111,70 @@ def read_token_file() -> str: raise e -bot = setup_bot() +# Note: pyright will complain about the async functions below not being accessed; +# this is a false positive, as the @bot.event decorators actually register and +# use the functions just fine. Unfortunately, it's not possible to silence the +# warning, so I'll have to deal with it. +# +# ruff doesn't complain, though. +def register_event_handlers(bot: commands.Bot): + @bot.event + async def on_error(event: str, *args, **kwargs): + _log.error( + f"Encountered unexpected exception while processing event {event} ({args = }; {kwargs = }):", + exc_info=sys.exc_info(), + ) + @bot.event + async def on_ready(): + _log.info("Ready: Connected to Discord") -@bot.event -async def on_error(event: str, *args, **kwargs): - _log.error( - f"Encountered unexpected exception while processing event {event} ({args = }; {kwargs = }):", - exc_info=sys.exc_info(), - ) + @bot.event + async def on_resumed(): + _log.info("Resumed: Session with Discord resumed") + @bot.event + async def on_shard_ready(shard_id: int): + _log.info(f"Ready: Shard ID {shard_id} connected to Discord") -@bot.event -async def on_ready(): - _log.info("Ready: Connected to Discord") + @bot.event + async def on_shard_resumed(shard_id: int): + _log.info(f"Resumed: Shard ID {shard_id} resumed session with Discord") + @bot.event + async def on_message(message: discord.Message): + _log.debug(f"Read message ({message.id = }): {message.content}") -@bot.event -async def on_resumed(): - _log.info("Resumed: Session with Discord resumed") + @bot.event + async def on_command(ctx: commands.Context): + _log.debug( + f"Command invoked:" + f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }" + ) + @bot.event + async def on_command_completion(ctx: commands.Context): + _log.debug( + f"Command completed:" + f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }" + ) -@bot.event -async def on_shard_ready(shard_id: int): - _log.info(f"Ready: Shard ID {shard_id} connected to Discord") - - -@bot.event -async def on_shard_resumed(shard_id: int): - _log.info(f"Resumed: Shard ID {shard_id} resumed session with Discord") - - -@bot.event -async def on_message(message: discord.Message): - _log.debug(f"Read message ({message.id = }): {message.content}") - - -@bot.event -async def on_command(ctx: commands.Context): - _log.debug( - f"Command invoked:" - f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }" - ) - - -@bot.event -async def on_command_completion(ctx: commands.Context): - _log.debug( - f"Command completed:" - f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }" - ) - - -@bot.event -async def on_command_error(ctx: commands.Context, error: commands.CommandError): - _log.error( - f"Failed to run command:" - f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }", - exc_info=error, - ) + @bot.event + async def on_command_error(ctx: commands.Context, error: commands.CommandError): + _log.error( + f"Failed to run command:" + f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }", + exc_info=error, + ) async def main(): _log.debug("Entered main()") + bot = setup_bot() + + register_event_handlers(bot) + token = read_token_file() async with bot: