Skip to content

Commit

Permalink
Update app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Mtp__ authored Oct 12, 2024
1 parent 109e115 commit 4900666
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ async def respect(interaction: discord.Interaction, member: discord.Member):
write_local_data(data)
await interaction.response.send_message(f"{member.name} has been given respect! 🪙 They now have {data[str(guild_id)][str(user_id)]['respect_count']} respect points.")

# /leaderboard command to show top users by respect points
# /leaderboard command to show top users by respect points with error handling and better data handling
@client.tree.command(name="leaderboard", description="Show top users by respect points")
async def leaderboard(interaction: discord.Interaction):
guild_id = interaction.guild.id
Expand All @@ -138,7 +138,8 @@ async def leaderboard(interaction: discord.Interaction):
leaderboard = cursor.fetchall()
else:
data = read_local_data()
leaderboard = sorted(data[str(guild_id)].items(), key=lambda x: x[1]['respect_count'], reverse=True)[:10]
guild_data = data.get(str(guild_id), {})
leaderboard = sorted(guild_data.items(), key=lambda x: x[1]['respect_count'], reverse=True)[:10]

embed = discord.Embed(
title="Respect Leaderboard",
Expand All @@ -147,8 +148,18 @@ async def leaderboard(interaction: discord.Interaction):
)

for rank, (user_id, respect_count) in enumerate(leaderboard, 1):
member = await interaction.guild.fetch_member(int(user_id)) if USE_MYSQL else await interaction.guild.fetch_member(int(user_id[0]))
embed.add_field(name=f"{rank}. {member.name}", value=f"{respect_count[1]['respect_count'] if not USE_MYSQL else respect_count} respect points 🪙", inline=False)
try:
if USE_MYSQL:
member = await interaction.guild.fetch_member(int(user_id))
points = respect_count # In MySQL, respect_count is directly available
else:
member = await interaction.guild.fetch_member(int(user_id))
points = respect_count['respect_count'] # For local storage

embed.add_field(name=f"{rank}. {member.name}", value=f"{points} respect points 🪙", inline=False)
except discord.NotFound:
# Skip users who cannot be found in the guild
continue

await interaction.response.send_message(embed=embed)

Expand Down

0 comments on commit 4900666

Please sign in to comment.