shaiwatcher/modules/usage/usage_stats.py
Franz Rolfsvaag 77f92abe19 0.4.1.0.a8
- Converted all commands to slash-only commands
2025-08-16 03:12:33 +02:00

37 lines
1.3 KiB
Python

# modules/usage/usage_stats.py
from __future__ import annotations
from discord.ext import commands
import discord
COUNTER_KEY_PREFIX = "cmd::"
def _key_from_app(cmd: discord.app_commands.Command) -> str:
return f"{COUNTER_KEY_PREFIX}{getattr(cmd, 'qualified_name', None) or getattr(cmd, 'name', 'unknown')}"
class UsageStatsCog(commands.Cog):
"""Slash-only metrics; count once per successful app command completion."""
def __init__(self, bot: commands.Bot):
self.bot = bot
print("[usage] UsageStatsCog init (slash-only)")
@commands.Cog.listener()
async def on_app_command_completion(self, interaction: discord.Interaction, command: discord.app_commands.Command):
dm = getattr(self.bot, "data_manager", None)
if not dm:
return
try:
key = _key_from_app(command)
newv = dm.incr_counter(key, 1)
print(f"[usage] app ++ {key} -> {newv}")
except Exception as e:
print("[usage] app !! incr failed:", repr(e))
async def setup(bot: commands.Bot):
if getattr(bot, "_usage_stats_loaded", False):
print("[usage] UsageStatsCog already loaded; skipping duplicate add")
return
await bot.add_cog(UsageStatsCog(bot))
bot._usage_stats_loaded = True
print("[usage] UsageStatsCog loaded (slash-only)")