我想基于硬币系统制作排行榜。
我尝试了以下方法:sorted(lbcashusers, key=lambda x: lbcashusers[x].get('Total', 0), reverse=True)
但这不起作用,我被卡住了。我希望Total
在嵌入消息中列出排行榜。
现在我的代码:
@bot.command(aliases=['lb', 'top'])
async def leaderboard(ctx, choice = None):
if choice == '-cash':
with open('casinomoney.json', 'r') as flbcash:
lbcashusers = json.load(flbcash)
lbcash = sorted(lbcashusers, key=lambda x: lbcashusers[x].get('Cash', 0), reverse=True)
message = ''
for number, user in enumerate(lbcash):
message += '{0}. {1} with {2}Cash\n'.format(number + 1, user, lbcash[user].get('Cash', 0))
await ctx.send(message)
我的Json文件:
{
"146293573422284800": {
"Bank": 1000,
"Cash": 1,
"Total": 1001
},
"155149108183695360": {
"Bank": 2000,
"Cash": 1,
"Total": 2001
},
"270904126974590976": {
"Bank": 3000,
"Cash": 1,
"Total": 3001
}
}