【问题标题】:How can I make top 10 command work, where it grabs the top 10 in the JSON?如何使前 10 个命令起作用,它在 JSON 中获取前 10 个命令?
【发布时间】: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


    【解决方案1】:

    错误似乎是由于experience = self.getexperience() 行引起的,正如完整的回溯应该指出的那样。你的类/cog 没有方法,getexperience

    【讨论】:

    • 我已经意识到了,但我不知道如何处理它
    • getexperience 应该是什么?将其添加为方法?
    猜你喜欢
    • 1970-01-01
    • 2017-09-01
    • 2017-01-23
    • 2020-05-19
    • 2020-09-27
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-03
    相关资源
    最近更新 更多