我正在尝试创建一个排行榜,以显示在向不和谐聊天室发送消息时用户收集的不同统计信息。我正在通过JSON文件存储数据,想知道我是否在正确的轨道上,或者是否有更好的方法来显示带有Embed的统计信息?
编辑:编辑代码以显示所做的更改。新问题是,我如何显示11-20并向前翻一页?尝试了下面的代码,但是当显示11-20时,它发送#11作为嵌入,然后发送#11-12作为嵌入,依此类推,等等,并且不停止于20,只是继续发送更多的嵌入。 >
client = discord.Client()
try:
with open("cash.json") as fp:
cash = json.load(fp)
except Exception:
cash = {}
def save_cash():
with open("cash.json", "w+") as fp:
json.dump(cash, fp, sort_keys=True, indent=4)
def add_dollars(user: discord.User, dollars: int):
id = user.id
if id not in cash:
cash[id] = {}
cash[id]["dollars"] = cash[id].get("dollars", 0) + dollars
print("{} now has {} dollars".format(user.name, cash[id]["dollars"]))
save_cash()
def get_dollars(user: discord.User):
id = user.id
if id in cash:
return cash[id].get("dollars", 0)
return 0
client.event
async def on_message(message):
if message.content.startswith('!lb cash'):
cash_leaderboard = sorted(cash, key=lambda x : cash[x].get('dollars', 0), reverse=True)
msg = ''
for number, user in enumerate(cash_leaderboard):
msg += '{0}. <@!{1}> {2} Dollars\n\n'.format(number +1, user, cash[user].get('dollars', 0))
if number == 10:
break
else:
number += 1
embed = discord.Embed(
title="TOP BALLER LEADERBOARD\nBallers:",
color=0x24d7cf,
description=msg
)
embed.set_author(name="BOT", icon_url="")
embed.set_thumbnail(url="")
embed.set_footer(text="BOT", icon_url="")
lb_msg = await client.send_message(message.channel, embed=embed)
await client.add_reaction(lb_msg, "⏩")
lb_wait = await client.wait_for_reaction(emoji="⏩", user=message.author, message=lb_msg, timeout=1800)
if lb_wait:
await client.delete_message(lb_msg)
cash_leaderboard2 = sorted(cash, key=lambda x : cash[x].get('dollars', 0), reverse=True)
msg = ''
for number, user in enumerate(cash_leaderboard2):
msg += '{0}. <@!{1}> {2} Dollars\n\n'.format(number +11, user, cash[user].get('dollars', 0))
if number == 20:
break
else:
number += 1
embed = discord.Embed(
title="TOP BALLER LEADERBOARD 11-20\nBallers:",
color=0x24d7cf,
description=msg
)
embed.set_author(name="BOT", icon_url="")
embed.set_thumbnail(url="")
embed.set_footer(text="BOT", icon_url="")
await client.send_message(message.channel, embed=embed)