繁体   English   中英

(discord.py) keyError: '钱包'

[英](discord.py) keyError: 'wallet'

@bot.command()
async def balance(ctx):
    await open_account(ctx.author)
    user = ctx.author
    users = await get_bank_data()

    wallet_amt = users[str(user.id)]["wallet"]
    bank_amt = users[str(user.id)]["bank"]

    em = discord.Embed(title=f"{ctx.author.name}'s balance")
    em.add_field(name="Wallet", value = wallet_amt)
    em.add_field(name="Bank", value = bank_amt)
    await ctx.send(embed=em)

@bot.command()
async def beg(ctx):
    await open_account(ctx.author)
    users = await get_bank_data()

    earnings = random.randrange(100)
    await ctx.send(f"someone gave you {earnings} coins.")

    users[str(user.id)]["wallet"] += earnings
    with open('mainbank.json',"w") as f:
        json.dump(users,f)

async def open_account(user):
users = await get_bank_data()

if str(user.id) in users:
    return False

users[str(user.id)] = {"Wallet": 0, "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

它说关键文件“C:/Users/Admin/Desktop/bot/vibebot.py”,第 78 行,余额 wallet_amt = users[str(user.id)]["wallet"] KeyError: 'wallet'

我不是这类代码的专家,所以任何帮助都会非常感谢!

这意味着“钱包”不是字典中的键,因此请确保您的 JSON 结构如此。

{“users”: {“wallet”: 9}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM