我正在尝试为排行榜提供“向前翻页”功能,该功能将显示排序后的json数据中的数字11-20。现在它确实显示11-20,但是当它发送时,它将首先发送#11,然后在另一条消息中发送#11和#12,然后在另一条消息中发送#11,#12和#13,依此类推,等等
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)
msg2 = ''
for number, user in enumerate(cash_leaderboard2):
if number >= 10:
msg2 += '{0}. <@!{1}> {2} Dollars\n\n'.format(number +1, user, cash[user].get('dollars', 0))
if number == 20:
break
embed2 = discord.Embed(
title="TOP BALLER LEADERBOARD 11-20\nBallers:",
color=0x24d7cf,
description=msg2
)
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=embed2)