I created a level system in discord.py, the data is saved in json. I have a command that displays the level of a given user, but I don't know how to make the top 5 users with the highest level. I haven't found how to do it anywhere.
This is my code from levels system:
async def update_data(users, user, server):
if not str(server.id) in users:
users[str(server.id)] = {}
if not str(user.id) in users[str(server.id)]:
users[str(server.id)][str(user.id)] = {}
users[str(server.id)][str(user.id)]['experience'] = 0
users[str(server.id)][str(user.id)]['level'] = 1
elif not str(user.id) in users[str(server.id)]:
users[str(server.id)][str(user.id)] = {}
users[str(server.id)][str(user.id)]['experience'] = 0
users[str(server.id)][str(user.id)]['level'] = 1
async def add_experience(users, user, exp, server):
users[str(user.guild.id)][str(user.id)]['experience'] += exp
async def level_up(users, user, channel, server):
experience = users[str(user.guild.id)][str(user.id)]['experience']
lvl_start = users[str(user.guild.id)][str(user.id)]['level']
lvl_end = int(experience ** (1/4))
if str(user.guild.id) != '757383943116030074':
if lvl_start < lvl_end:
await channel.send('{} has leveled up to Level {}'.format(user.mention, lvl_end))
users[str(user.guild.id)][str(user.id)]['level'] = lvl_end
JSON file:
{"678938477710278667": {"experience": 692, "level": 5}, "627244746729062421": {"experience": 48, "level": 2}, "826822736340713522": {"678938477710278667": {"experience": 2548, "level": 7}, "627244746729062421": {"experience": 16, "level": 2}}}