【问题标题】:i am getting KeyError: '563690668808208423' when i'm trying to add exp to user当我尝试向用户添加 exp 时,我收到 KeyError: '563690668808208423'
【发布时间】:2020-11-22 05:15:58
【问题描述】:

我收到 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 是我的身份证

【问题讨论】:

  • 可以在这里打印错误信息吗?

标签: python json discord


【解决方案1】:

您只需将其更改为以下内容:

async def add_experience(users, user, exp):
    users[str(user.id)]['experience'] += exp

从用户返回的 id 是 user id 以 int(整数)形式返回,因此您必须先将其更改为字符串

【讨论】:

  • 添加后需要在JSON文件中重新编写体验
猜你喜欢
  • 2020-09-28
  • 1970-01-01
  • 1970-01-01
  • 2014-06-15
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-17
相关资源
最近更新 更多