【发布时间】:2019-12-19 11:36:46
【问题描述】:
这是我当前的代码:
import discord
from discord.ext import commands
class Fun:
def __init__(self, client):
self.client = client
@commands.command(aliases=["lb"], pass_context=True)
async def leaderboard(self, ctx):
experience = self.getexperience()
with open('users.json') as f:
data = json.load(f)
lines = sorted(data.items(), key=operator.itemgetter(1), reverse=True) # sorts lines by balance
lb = [] # leaderboard
for line in lines: # each line in file
user = self.bot.get_user(id=int(line[0])) # grab user object from ID
if not user:
user = await self.bot.fetch_user(int(line[0]))
lb.append(f"{user.name} | {format(line[1], ',d')} {experience}\n") # add username and balance to leaderboard
limitedMsgs = [] # array for each page
pageCount = math.ceil(len(lb) / 10) # pageCount = number of users / 10 users per page
for x in range(0, pageCount): # for every page
limitedMsgs.append("".join(lb[x*10:x*10+10])) # add those 10 users to 1 page
currPage = 0
embed = discord.Embed(color=0xdfe324, description=limitedMsgs[0])
embed.set_footer(text=f"Page: {currPage + 1} of {pageCount}")
msg = await self.client.say(embed=embed)
def check(reaction, user):
return (user == ctx.message.author) and (str(reaction.emoji) == '⬅' or str(reaction.emoji) == '➡')
while(True):
if pageCount > 1:
if currPage == 0: # if first page
await msg.add_reaction("➡")
print(pageCount)
elif (currPage + 1) == pageCount: # if last page
await msg.add_reaction("⬅")
else: # if not first nor last
await msg.add_reaction("➡")
await msg.add_reaction("⬅")
try:
reaction, user = await self.bot.wait_for('reaction_add', timeout=60, check=check) # wait for user reaction
except asyncio.TimeoutError:
break # end while loop if no user reaction
if str(reaction.emoji) == '⬅':
currPage = currPage - 1
if str(reaction.emoji) == '➡':
currPage = currPage + 1
embed = discord.Embed(color=0xdfe324, description=limitedMsgs[currPage])
embed.set_footer(text=f"Page: {currPage + 1} of {pageCount}")
await msg.clear_reactions()
await msg.edit(embed=embed)
await msg.clear_reactions() # clear reactions after while loop
def setup(client):
client.add_cog(Fun(client))
这是错误:
discord.ext.commands.errors.CommandInvokeError: 命令引发异常:AttributeError: 'Fun' object has no attribute 'getexperience'
就像我之前说的,我希望它获取 JSON 中的前 10 名,然后将其排序到最后,但我被困在这部分。大部分代码已经完成;它只是从 JSON 中获取 xp。任何帮助,将不胜感激。谢谢。
【问题讨论】:
标签: python json discord.py