MemeySuxzAtCoding
MemeySuxzAtCoding

Reputation: 11

'JSONDecodeError: Expecting value' Error while adding leveling system to bot

I (unfortunately) don't have a background in coding, so I use coding blocks. I've tried using a levelling system code I got from someone, but it only worked once. But it keeps showing an error now.

Code

@bot.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)
  
@bot.event
async def on_message(message):
  await bot.process_commands(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)
    
    
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):
  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

Error

Ignoring exception in on_message
Traceback (most recent call last):
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.7/site-packages/discord/client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "<string>", line 40, in on_message
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.7/json/__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.7/json/__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.7/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/data/user/0/ru.iiec.pydroid3/files/arm-linux-androideabi/lib/python3.7/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0...

Not sure why it all of a sudden stopped. I (might) also need help with a code with a 'economy system' with privileges as the bot's creator. I'm still a beginner.

Upvotes: 1

Views: 121

Answers (1)

Diggy.
Diggy.

Reputation: 6944

If you're not too experienced with Python, then I can recommend reading up on some tutorials online, such as Automate the boring stuff. You'll be up and running in no time.

Here I've listed a few things that I've noticed in your code.


Quotation marks:

First of all, you seem to be missing a couple of quotation marks:

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

Should be:

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

You can tell if/where you're missing some quotation marks, because you'll have a big block of code that's all the same colour which starts and ends with quotation marks.


Indentation:

I'm not sure if this is SO's fault when you pasted in your code or what, but your indentation is also a bit funky:

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

Should instead be:

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

Try some exercises on indentation here if you're not entirely familiar with it just yet.

Another example of some fixed indentation on your code:

@bot.event
async def on_message(message):
    await bot.process_commands(message)
    if not message.author.bot: # also edited for more pythonic use of if statements
        with open('users.json', 'r') as f:
        users = json.load(f)

The error at hand

JSONDecodeError usually appears when it's reading in a badly-formatted JSON. This could happen for a multitude of reasons, but I happen to suspect that in this case it's because you tried dumping an object that didn't exist, or wasn't correctly formatted (specifically, json.dump(users, f)), and it closed without saving any data.

And when that happens, you end up with a .json that looks like this:

{

At that point, it's a good idea to delete the .json and start your code again - this is why making backups is so important!

If you're still unsure about any of this, I'll be happy to add some more to my answer, just leave a comment!


References:

Upvotes: 1

Related Questions