猿问

KeyError:discord.py 中的“594750729810477063”

await open_account(ctx.author)

    users = await get_bank_data()


    earnings = (a_var3)


    users[str(discord.user.id)]["Simp Wallet"] += earnings


@bot.command()

async def balance(ctx):

    await open_account(ctx.author)


    users = await get_bank_data()


    wallet_amt = users[str(user.id)]["Simp Wallet"] = 0

    bank_amt =  users[str(user.id)]["Simp Bank"] = 0



    em = discord.Embed(title = f"{ctx.author.name}'s Balance")

    em.add_field(name = "Simp Wallet", value = wallet_amt)

    em.add_field(name = "Simp Bank", value = bank_amt)

    await ctx.send(embed = em)



async def open_account(user):

    users = await get_bank_data()


    if str(user.id) is users:

        return False

    else:

        users[str(user.id)]["Simp Wallet"] = 0

        users[str(user.id)]["Simp Bank"] = 0


    with open('mainbank.json',"w") as f:

        json.dump(users,f)

    return True


async def get_bank_data():

    with open('mainbank.json',"r") as f:

        users = json.load(f)

    return users

Discord.ext.commands.errors.CommandInvokeError:命令引发异常:KeyError:'594750729810477063'。就是这么说的。我不是这方面的专家,所以,任何帮助将不胜感激!


完整回溯如下:


The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke

    await ctx.command.invoke(ctx)

  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke

    await injected(*ctx.args, **ctx.kwargs)

  File "C:\Users\Admin\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped

    raise CommandInvokeError(exc) from exc

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: KeyError: '345878244756684801'


幕布斯6054654
浏览 76回答 1
1回答

qq_花开花谢_0

您的open_account函数看起来不会执行您需要执行的操作。如果还没有给定用户的新帐户,您需要它来创建一个新帐户。但是您没有任何代码来检查现有用户(看起来您正在尝试,但有一个拼写错误),并且您也没有代码来创建内部字典。这就是我认为你想要的:async def open_account(user):    users = await get_bank_data()    if str(user.id) in users:                         # note, use "in" here rather than "is"        return False    users[str(user.id)] = {"Simp Wallet": 0, "Simp Bank": 0}  # create inner dictionary here    with open('mainbank.json',"w") as f:        json.dump(users,f)    return True
随时随地看视频慕课网APP

相关分类

Python
我要回答