Implement a leaderboard command

This commit is contained in:
TropiiDev 2025-02-16 21:12:53 -05:00
parent 6f6554a488
commit 4b7f9cce77
2 changed files with 26 additions and 6 deletions

View File

@ -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):

View File

@ -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}})