i am getting KeyError: '563690668808208423' when i'm trying to add exp to user

Viewed 44

i am getting KeyError: '563690668808208423', i am trying to add exp to user when the user uses command

@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

i am using json file as database:

{"563690668808208423": {"experience": 0, "level": 1, "job": "Cleaner"},
"517371726720532511": {"experience": 0, "level": 1, "job": "Cleaner"}}

563690668808208423 is my ID

1 Answers

You should just change it to the following:

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

The id returned from the user which is the user id is returned as an int (integer) so you have to change it to a string first

Related