bot: main: register event handlers inside separate function
Makes the overall program flow nicer. Signed-off-by: Max R. Carrara <max@aequito.sh>
This commit is contained in:
parent
14be2289a9
commit
80918f7a42
1 changed files with 51 additions and 51 deletions
|
|
@ -111,9 +111,13 @@ def read_token_file() -> str:
|
||||||
raise e
|
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
|
@bot.event
|
||||||
async def on_error(event: str, *args, **kwargs):
|
async def on_error(event: str, *args, **kwargs):
|
||||||
_log.error(
|
_log.error(
|
||||||
|
|
@ -121,32 +125,26 @@ async def on_error(event: str, *args, **kwargs):
|
||||||
exc_info=sys.exc_info(),
|
exc_info=sys.exc_info(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
_log.info("Ready: Connected to Discord")
|
_log.info("Ready: Connected to Discord")
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_resumed():
|
async def on_resumed():
|
||||||
_log.info("Resumed: Session with Discord resumed")
|
_log.info("Resumed: Session with Discord resumed")
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_shard_ready(shard_id: int):
|
async def on_shard_ready(shard_id: int):
|
||||||
_log.info(f"Ready: Shard ID {shard_id} connected to Discord")
|
_log.info(f"Ready: Shard ID {shard_id} connected to Discord")
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_shard_resumed(shard_id: int):
|
async def on_shard_resumed(shard_id: int):
|
||||||
_log.info(f"Resumed: Shard ID {shard_id} resumed session with Discord")
|
_log.info(f"Resumed: Shard ID {shard_id} resumed session with Discord")
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_message(message: discord.Message):
|
async def on_message(message: discord.Message):
|
||||||
_log.debug(f"Read message ({message.id = }): {message.content}")
|
_log.debug(f"Read message ({message.id = }): {message.content}")
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_command(ctx: commands.Context):
|
async def on_command(ctx: commands.Context):
|
||||||
_log.debug(
|
_log.debug(
|
||||||
|
|
@ -154,7 +152,6 @@ async def on_command(ctx: commands.Context):
|
||||||
f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }"
|
f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_command_completion(ctx: commands.Context):
|
async def on_command_completion(ctx: commands.Context):
|
||||||
_log.debug(
|
_log.debug(
|
||||||
|
|
@ -162,7 +159,6 @@ async def on_command_completion(ctx: commands.Context):
|
||||||
f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }"
|
f" {ctx.command = }; {ctx.message.author = }; {ctx.message.id = }"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
|
async def on_command_error(ctx: commands.Context, error: commands.CommandError):
|
||||||
_log.error(
|
_log.error(
|
||||||
|
|
@ -175,6 +171,10 @@ async def on_command_error(ctx: commands.Context, error: commands.CommandError):
|
||||||
async def main():
|
async def main():
|
||||||
_log.debug("Entered main()")
|
_log.debug("Entered main()")
|
||||||
|
|
||||||
|
bot = setup_bot()
|
||||||
|
|
||||||
|
register_event_handlers(bot)
|
||||||
|
|
||||||
token = read_token_file()
|
token = read_token_file()
|
||||||
|
|
||||||
async with bot:
|
async with bot:
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue