Skip to content

Commit 5e5356c

Browse files
committed
feat: log command calls
1 parent 03752c2 commit 5e5356c

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/commands/club.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ async def club_infos(
5252
@app_commands.command(name="add_member")
5353
@app_commands.autocomplete(club=autocomplete_club)
5454
async def add_club_member(
55-
self,
56-
interaction: Interaction,
57-
club: int,
58-
member: Member,
55+
self, interaction: Interaction, club: int, member: Member
5956
):
6057
await interaction.response.defer(thinking=True)
6158
club = next(

src/commands/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111

1212
class MiscCog(commands.Cog):
1313
@app_commands.command(name="ping")
14-
async def sync_commands(self, interaction: Interaction[AeBot]):
14+
async def ping(self, interaction: Interaction[AeBot]):
1515
await interaction.response.send_message("pong")

src/main.py

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
from __future__ import annotations
2+
13
import asyncio
24
import logging
5+
from datetime import datetime
6+
from typing import TYPE_CHECKING
37

4-
from discord import Guild, Intents
8+
from discord import Guild, Intents, Interaction
59
from discord.ext import commands
610
from discord.utils import setup_logging
711

@@ -11,6 +15,10 @@
1115
from src.commands.misc import MiscCog
1216
from src.settings import Settings
1317

18+
if TYPE_CHECKING:
19+
from discord.app_commands import Command
20+
from discord.ext.commands import Context
21+
1422

1523
class AeBot(commands.Bot):
1624
watched_guild: Guild
@@ -33,6 +41,28 @@ async def on_ready(self):
3341
self.watched_guild = self.get_guild(self.settings.guild.id)
3442
self.logger.info(f"Bot ready to act on {self.watched_guild.name}")
3543

44+
async def on_command(self, ctx: Context):
45+
start = ctx.message.created_at
46+
duration = datetime.now(tz=start.tzinfo) - start
47+
self.logger.info(
48+
f"Command `{ctx.command.name}` called by {ctx.author.name} "
49+
f"[{duration.total_seconds():.6f}sec]"
50+
)
51+
52+
async def on_app_command_completion(
53+
self, interaction: Interaction, command: Command
54+
):
55+
start = interaction.created_at
56+
duration = datetime.now(tz=start.tzinfo) - start
57+
cmd_name = (
58+
f"{command.parent.name} {command.name}" if command.parent else command.name
59+
)
60+
self.logger.info(
61+
f"Slash command `{cmd_name}` "
62+
f"called by {interaction.user.name} in {interaction.channel.name} "
63+
f"[{duration.total_seconds():.6f}sec]"
64+
)
65+
3666

3767
async def main():
3868
async with SithClient() as client:

0 commit comments

Comments
 (0)