From 4b7f9cce77a9f5c2a8c76322252ed515b9199ceb Mon Sep 17 00:00:00 2001 From: TropiiDev Date: Sun, 16 Feb 2025 21:12:53 -0500 Subject: [PATCH] Implement a leaderboard command --- commands/quiz.py | 28 ++++++++++++++++++++++++---- lib/leaderboard_helper.py | 4 ++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/commands/quiz.py b/commands/quiz.py index 86ebf98..f66ef78 100644 --- a/commands/quiz.py +++ b/commands/quiz.py @@ -79,11 +79,31 @@ class Quiz(commands.Cog): async def on_ready(self): print("Quiz online") - quiz = app_commands.Group(name="quiz", description="All the commands related to quiz") + #quiz = app_commands.Group(name="quiz", description="All the commands related to quiz") + + # command might not work as of now but if it does have an issue ill fix and remove comment + # if the comment is still here just assume it worked first try + @app_commands.command(name="leaderboard", description="See who has the highest amount of questions correct") + async def leaderboard(self, interaction: discord.Interaction, theme: str): + coll = db.leaderboard + guild = coll.find_one({"guild_id": interaction.guild.id}) + + if guild is None: + await interaction.response.send_message("An error has occurred..") + return + + if coll.count_documents({"guild_id": interaction.guild.id}) < 10: + await interaction.response.send_message("Not enough users to make a leaderboard") + return + + leaderboard = coll.find({"guild_id": interaction.guild.id}).sort(f"{theme}", pymongo.DESCENDING).limit(10) + + em = discord.Embed(title="Quiz Leaderboard", description="Top 10", color=interaction.user.color) + for i, user in enumerate(leaderboard): + em.add_field(name=f"{i}. {self.bot.get_user(user['_id'])}", value=f"Correct: {user[f"{theme}"]["amount_correct"]}") + + await interaction.response.send_message(embed=em) - @quiz.command(name="leaderboard", description="See who has the highest amount of questions correct") - async def leaderboard(self, interaction: discord.Interaction): - await interaction.response.send_message(f"This command is not finished") @app_commands.command(name="quiz", description="Test your knowledge") async def quiz(self, interaction: discord.Interaction): diff --git a/lib/leaderboard_helper.py b/lib/leaderboard_helper.py index 1c5b3af..e8fe65b 100644 --- a/lib/leaderboard_helper.py +++ b/lib/leaderboard_helper.py @@ -5,11 +5,11 @@ client = pymongo.MongoClient(os.getenv('mongo_url')) db = client.quiz coll = db.leaderboard -def increment_correct(user_id, theme): +def increment_correct(user_id, guild_id , theme): user = coll.find_one({"_id": user_id}) if user is None: - coll.insert_one({"_id": user_id, f"{theme}": { "amount_correct": 1}}) + coll.insert_one({"_id": user_id, "guild_id": guild_id, f"{theme}": { "amount_correct": 1}}) return True coll.update_one({"_id": user_id}, {"$inc": {f"{theme}.amount_correct": 1}})