Skip to content

Commit

Permalink
Fix: add logging in on_command event instead of in a function passed …
Browse files Browse the repository at this point in the history
…to before_invoke decorator
  • Loading branch information
J3y0 committed Jul 30, 2023
1 parent 089148e commit 555d01c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
6 changes: 6 additions & 0 deletions src/bot/root_pythia_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,9 @@ async def on_error(event, *args, **kwargs):
await BOT.channel.send(f"{event} event failed, please check logs for more details")

BOT.logger.exception("Unhandled exception in '%s' event", event)


@BOT.event
async def on_command(ctx):
"""Add logging when a command is triggered by a user"""
BOT.logger.info("'%s' command triggered by '%s'", ctx.command, ctx.author)
11 changes: 0 additions & 11 deletions src/bot/root_pythia_cogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,6 @@ def __init__(self, bot, dbmanager):
# pylint: disable-next=no-member
self.check_new_solves.start()

async def log_command_call(self, ctx):
# Maybe we should use the on_command event
# https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=cog#discord.discord.ext.commands.on_command
# the logging would be implicit, no need of a redundant @commands.before_invoke(...) on
# each command. But right now I prefer to stick with this explicit solution
self.logger.info("'%s' command triggered by '%s'", ctx.command, ctx.author)

@commands.before_invoke(log_command_call)
@commands.command(name="status")
async def status(self, ctx):
"""
Expand Down Expand Up @@ -87,7 +79,6 @@ async def base_add_one_user(self, ctx, user_id: int):
self.logger.info("Add user '%s'", user)
await ctx.message.channel.send(f"{user} added!\nPoints: {user.score}")

@commands.before_invoke(log_command_call)
@commands.command(name="addusers")
async def addusers(self, ctx, *user_ids):
"""
Expand All @@ -109,15 +100,13 @@ async def addusers(self, ctx, *user_ids):
# should continue here (ex: multiple users but only one can be wrong)
await self.base_add_one_user(ctx, user_id)

@commands.before_invoke(log_command_call)
@commands.command(name="adduser")
async def adduser(self, ctx, user_id: int):
"""
Add rootme user with <user_id> to the tracked users
"""
await self.base_add_one_user(ctx, user_id)

@commands.before_invoke(log_command_call)
@commands.command(name="getuser")
async def getuser(self, ctx, user_id: int):
"""
Expand Down

0 comments on commit 555d01c

Please sign in to comment.