0

I have looked over all of the internet but am not able to solve this error. I have no idea what it means as I'm new to json files and python. This is for my Discord.py leveling system. Please help me. I don't Understand it at all. Its quite new to me. I am really confused. It doesn't make any sense to me.

Here is my code:

@client.event
async def on_member_join(member):
  with open('users.json', 'r') as f:
    users = json.load(f)

  await update_data(users, member)

with open('users.json', 'w') as f:
    json.dump(users, f)


@client.event
async def on_message(message):
  if message.author.bot == False:
      with open('users.json', 'r') as f:
          users = json.load(f)

    await update_data(users, message.author)
    await add_experience(users, message.author, 5)
    await level_up(users, message.author, message)

    with open('users.json', 'w') as f:
        json.dump(users, f)

await client.process_commands(message)


async def update_data(users, user):
    if not f'{user.id}' in users:
        users[f'{user.id}'] = {}
        users[f'{user.id}']['experience'] = 0
        users[f'{user.id}']['level'] = 1


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


async def level_up(users, user, message):
    with open('levels.json', 'r') as g:
        levels = json.load(g)
    experience = users[f'{user.id}']['experience']
    lvl_start = users[f'{user.id}']['level']
    lvl_end = int(experience ** (1 / 4))
if lvl_start < lvl_end:
    await message.channel.send(f'{user.mention} has leveled up to level {lvl_end}')
    users[f'{user.id}']['level'] = lvl_end

@client.command()
async def level(ctx, member: discord.Member = None):
    if not member:
        id = ctx.message.author.id
        with open('users.json', 'r') as f:
            users = json.load(f)
        lvl = users[str(id)]['level']
        await ctx.send(f'You are at level {lvl}!')
    else:
        id = member.id
        with open('users.json', 'r') as f:
            users = json.load(f)
        lvl = users[str(id)]['level']
        await ctx.send(f'{member} is at level {lvl}!')
levels.json file:
{}
users.json file:
{}
1
  • 1
    You'll get that error if the json file is empty. Commented Nov 9, 2020 at 18:15

1 Answer 1

1

Try putting {} or [] to your json file

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.