如何使用python在discord.py上制作排行榜命令?

我一直在尝试和我的朋友一起编写Discord机器人,并且正在尝试执行排行榜命令。我一直在尝试修复命令并重新填充数字,但是没有任何效果。

评论
浅浅夏
浅浅夏

You don't want to sort dict.items() because then you'll be stuck finding the keys. You can use a class definition.

class LeaderBoardPosition:

    def __init__(self, user, coins):
        self.user = user
        self.coins = coins

并使用简单的排序功能(假设您的数据存储在“硬币”中):

leaderboards = []
for key, value in coins.items():
    leaderboards.append(LeaderBoardPosition(key, value))

top = sorted(leaderboards, key=lambda x: x.coins, reverse=True)

Now you have a list, named top with the data. Here is a simple leaderboard function.

await message.channel.send(f'**<< 1 >>** <@{top[0].user}> with {top[0].coins} coins')
try:
    await message.channel.send(f'**<< 2 >>** <@{top[1].user}> with {top[1].coins} coins')
    try:
        await message.channel.send(f'**<< 3 >>** <@{top[2].user}> with {top[2].coins} coins')
    except IndexError:
        await message.channel.send('There is no 3rd place yet.')
except IndexError:
    await message.channel.send('There is no 2nd place yet.')
点赞
评论