I was trying to make a leaderboard command for my level system on discord.py with JSON but for some weird reason, it won't work. Only the top 3 highest level players are shown instead of the top 5 and when I try to give a ton of exp to another player to change the 1st position the last 1st place player does not go to the 2nd place but is nowhere to be found.
Here is the output image, on the top is the output from when I hadn't changed Void's exp, and on the bottom is when I changed Void's exp.
# leaderboard command
@commands.command(name='leaderboard', aliases=['lb', 'top', 'levels'])
async def leaderboard(self, ctx, arg:str=None):
try:
with open(levelsJSON, 'r') as levels_file:
levels = json.load(levels_file)
## Top 5 places VARIABLES
first_name = 'N/A'
first_level = 0
first_exp = 0
second_name = 'N/A'
second_level = 0
second_exp = 0
third_name = 'N/A'
third_level = 0
third_exp = 0
fourth_name = 'N/A'
fourth_level = 0
fourth_exp = 0
fifth_name = 'N/A'
fifth_level = 0
fifth_exp = 0
## Variables END
# loops through the file
for user in levels:
exp = float(levels[user]['exp'])
level = float(levels[user]['level'])
name = levels[user]['name']
if exp > first_exp:
first_name = name
first_level = level
first_exp = exp
elif exp > second_exp:
second_name = name
second_level = level
second_exp = exp
elif exp > third_exp:
third_name = name
third_level = level
third_exp = exp
elif exp > fourth_exp:
fourth_name = name
fourth_level = level
fourth_exp = exp
elif exp > fifth_exp:
fifth_name = name
fifth_level = level
fifth_exp = exp
else:
pass
await ctx.send(f"1. {first_name}\n2. {second_name}\n3. {third_name}\n4. {fourth_name}\n5. {fifth_name}")
except Exception as error:
await ctx.send(f'```{error}```')
Here is the JSON File in which the data is stored.
{
"586579525849186315": {
"name": "MythCraftMC#0963",
"level": 8,
"exp": 2174.21875,
"max_exp": 2562.890625
},
"658633474995126282": {
"name": "Regney#2226",
"level": 3,
"exp": 100.0,
"max_exp": 337.5
},
"586579523549196315": {
"name": "Void#0363",
"level": 0,
"exp": 32345,
"max_exp": 100
},
"586579522449186315": {
"name": "Universal_Kid#0963",
"level": 4,
"exp": 350,
"max_exp": 500
},
"586572525844186315": {
"name": "ThatOneGuy#0453",
"level": 3,
"exp": 300,
"max_exp": 400
}
}
[ There are no ERRORS ]