Anthony
Anthony

Reputation: 83

Limit leaderboard to 10 users / Divide the leaderboard into pages

My current leaderboard shows every user that is in the dictionary, I would like to have a paged leaderboard, showing 10 users per page rather than showing every user in the leaderboard, but I can't really find a way to do so with my current code.

This is my current code:

  @commands.command()
  @commands.guild_only()
  @commands.cooldown(1, 3, commands.BucketType.guild)
  async def leaderboard(self, ctx):
    with open('json/servers/{Guild}/User.json'.format(Guild=ctx.guild.id), 'r') as f:
      users = json.load(f)

    leaderboard = {}
    total_amt = {}

    pages = []
  
    total_amt = {user:users[str(user)]['MONEY'] + users[str(user)]['BANK'] for user in users.keys()}
    leaderboard = {user: amount for user, amount in sorted(total_amt.items(), key=lambda item: item[1], reverse=True)}
    em = discord.Embed(title='Top de Dinero', color=0x96edfd)
    em.description = '\n'.join(f'**{list(leaderboard.keys()).index(user) + 1}.**' f"{'None' if self.bot.get_user(int(user)) == None else self.bot.get_user(int(user)).mention} " + f"**{amt}**" for user, amt in leaderboard.items())
    await ctx.send(embed=em)

Upvotes: 0

Views: 79

Answers (1)

Anthony
Anthony

Reputation: 83

Wow... Apparently it's easier than I thought, I was testing and found the solution to my problem, god how could I not think of it before.

Just had to modify this part a little:

em.description = '\n'.join(f'**{index + 1}.**' f"{'None' if self.bot.get_user(int(user)) == None else self.bot.get_user(int(user)).mention} " + f"**{amt}** {emoji}" for index, (user, amt) in enumerate(list(leaderboard.items())[:10]))

Upvotes: 1

Related Questions