summary refs log tree commit diff stats
path: root/cogs/stats.py
diff options
context:
space:
mode:
authorsuhas <hi@suhas.one>2023-11-16 23:00:29 -0600
committersuhas <hi@suhas.one>2023-11-16 23:00:29 -0600
commit2e38b5aefb08abdd34d5244d0998274e5f35ff3e (patch)
treea221637a7eaa9195c8ae5322d8c73c1ad70e90a2 /cogs/stats.py
parente0ede3dddabf27cde5a1db35e499885ece51f542 (diff)
downloadqbb-2e38b5aefb08abdd34d5244d0998274e5f35ff3e.tar.gz
add stats tracking
Diffstat (limited to 'cogs/stats.py')
-rw-r--r--cogs/stats.py36
1 files changed, 36 insertions, 0 deletions
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))