gpt4 book ai didi

python - 我收到 KeyError : '563690668808208423' when i'm trying to add exp to user

转载 作者:行者123 更新时间:2023-12-04 09:16:04 26 4
gpt4 key购买 nike

我收到 KeyError: '563690668808208423',我试图在用户使用命令时向用户添加 exp

@bot.command(pass_context=True)
async def work(ctx):
with open('data.json','r+', encoding = 'utf-8') as f:
kingdom = json.load(f)

if str(kingdom[str(ctx.author.id)]['job']) == "Cleaner":
await add_experience(kingdom, ctx.author ,10)
await ctx.send('Вы отлично поработали')
async def add_experience(users, user, exp):
users[user.id]['experience'] += exp
我使用 json 文件作为数据库:
{"563690668808208423": {"experience": 0, "level": 1, "job": "Cleaner"},
"517371726720532511": {"experience": 0, "level": 1, "job": "Cleaner"}}
563690668808208423是我的ID

最佳答案

您应该将其更改为以下内容:

async def add_experience(users, user, exp):
users[str(user.id)]['experience'] += exp
用户返回的 id 是 user id以 int(整数)形式返回,因此您必须先将其更改为字符串

关于python - 我收到 KeyError : '563690668808208423' when i'm trying to add exp to user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63207402/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com