From 2e38b5aefb08abdd34d5244d0998274e5f35ff3e Mon Sep 17 00:00:00 2001 From: suhas Date: Thu, 16 Nov 2023 23:00:29 -0600 Subject: add stats tracking --- cogs/stats.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 cogs/stats.py (limited to 'cogs/stats.py') diff --git a/cogs/stats.py b/cogs/stats.py new file mode 100644 index 0000000..03031ec --- /dev/null +++ b/cogs/stats.py @@ -0,0 +1,36 @@ +from discord.ext.commands import Cog, Bot +from discord.app_commands import command +from discord import Interaction, Embed, Color +from prisma import Prisma +from prisma.models import User, CategoryBreakdown +from common.types import category_field_translations + +reverse_categories = {v: k for k, v in category_field_translations.items()} + +class Stats(Cog): + def __init__(self, bot: Bot) -> None: + self.bot = bot + super().__init__() + + @command(description="Get your statistics for qbb") + async def stats(self, ctx: Interaction): + db = Prisma(auto_register=True) + await db.connect() + stats = await User.prisma().find_first(where={'id': ctx.user.id}) + cb = await CategoryBreakdown.prisma().find_first(where={'userId': ctx.user.id}) + if stats is None or cb is None: + embed = Embed(title="No Stats!", description="You have no stats! Trying using the bot and then running this command", color=Color.red()) + return await ctx.response.send_message(embed=embed) + embed = Embed(title="Your Stats!", description=f"""**Number of correct tossups:** {stats.questions_correct} + **Number of incorrect tossups:** {stats.questions_incorrect} + """) + print(stats.category_breakdown.__str__()) + for cat in category_field_translations.values(): + corr = getattr(cb, f'{cat}_correct') + incorr = getattr(cb, f'{cat}_incorrect') + embed.add_field(name=reverse_categories[cat], value=f'{corr+incorr} heard ({corr} correct)') + await ctx.response.send_message(embed=embed) + await db.disconnect() + +async def setup(bot: Bot) -> None: + await bot.add_cog(Stats(bot)) -- cgit 1.4.1-2-gfad0