Untitled

 avatar
unknown
plain_text
2 years ago
2.7 kB
4
Indexable
@tasks.loop(hours=1)
async def update_leaderboard():
    # Update the regular leaderboard
    top_users_main = collection_main.find().sort("xp", -1).limit(20)
    leaderboard_message_main = "### Hall of Fame\n"
    for idx, user_data in enumerate(top_users_main, start=1):
        user_id = user_data["_id"]
        try:
            member = await client.fetch_member(member.guild.id, int(user_id))
            xp = user_data["xp"]
            leaderboard_message_main += f"{idx}. {member.mention} - XP: {xp}\n"
            
            # Check and assign roles based on XP
            await check_and_assign_roles(member, xp, "main")
        except discord.NotFound:
            print(f"User with ID {user_id} not found.")

    channel = client.get_channel(leaderboard_channel_id)
    async for message in channel.history():
        if message.author == client.user and message.content.startswith("Top 20 Leaderboard:"):
            await message.edit(content=leaderboard_message_main)
            break
    else:
        await channel.send(leaderboard_message_main)

    # Update the Hall of Fame leaderboard
    top_users_hof = collection_hof.find().sort("xp", -1).limit(20)
    leaderboard_message_hof = "### Leaderboard\n"
    for idx, user_data in enumerate(top_users_hof, start=1):
        user_id = user_data["_id"]
        try:
            member = await client.fetch_member(member.guild.id, int(user_id))
            xp = user_data["xp"]
            leaderboard_message_hof += f"{idx}. {member.mention} - XP: {xp}\n"
        except discord.NotFound:
            print(f"User with ID {user_id} not found.")

    channel_hof = client.get_channel(hof_channel_id)  # Replace with the actual channel ID for Hall of Fame
    async for message in channel_hof.history():
        if message.author == client.user and message.content.startswith("Hall of Fame Leaderboard:"):
            await message.edit(content=leaderboard_message_hof)
            break
    else:
        await channel_hof.send(leaderboard_message_hof)

async def check_and_assign_roles(member, xp, context):
    if context == "main":
        for level, role_id in level_roles.items():
            if xp >= level * 3:  # Adjust the multiplier as needed
                role = member.guild.get_role(role_id)
                if role and role not in member.roles:
                    await member.add_roles(role)

                    # Congratulations for leveling up
                    congratulations_channel = member.guild.get_channel(congratulations_channel_id)
                    await congratulations_channel.send(f"Congratulations {member.mention}! You leveled up to level {level}!")
Editor is loading...
Leave a Comment