discord.ext.commands.errors.CommandNotFound:即使存在命令“balance”也未找到错误

时间:2020-12-19 13:14:55

标签: discord discord.py

我正在尝试使用经济系统制作一个 discord.py 机器人,这是显示余额的基本脚本。

client.command()
async def balance(ctx):
    global user

    await open(ctx.author)
    users = await get()

    wallet_data = users[str(user.id)]["wallet"]
    bank_data = users[str(user.id)]["bank"]

    details = discord.Embed(title=f"{ctx.author.name}'s balance now", color=discord.Color.red())
    details.add_field(name="W A L L E T", value=wallet_data)
    details.add_field(name="B A N K", value=bank_data)

    await ctx.send(embed=details)


async def open(user):
    global users

    users = await get()
    
    with open(r"C:\discbot\bank.json", "r") as data:
        users = json.load(data)
    
    if str(user.id) in users:
        return False
    
    else: 
        users[str(user.id)]["wallet"]=0
        users[str(user.id)]["bank"]=0
    
        with open(r"C:\discbot\bank.json", "w") as data:
            json.dump(users,data)
    
        return True


async def get():
    with open(r"C:\discbot\bank.json", "r") as data:
        users = json.load(data)

    return users


client.run(ID)

然而它返回

discord.ext.commands.errors.CommandNotFound: Command "balance" is not found

即使在我运行时定义了平衡。

1 个答案:

答案 0 :(得分:1)

您需要在 @client.command() 之前调用 async def ... 才能将其注册为命令。

@client.command()
async def foo(ctx):
    await ctx.send("bar")

另外,你重新定义了开放。因此,您的 async def open 函数在调用时将是递归的,并且会出错。