H
H
Hibiki Teitoku2020-11-12 13:52:26
Python
Hibiki Teitoku, 2020-11-12 13:52:26

There is a problem with json.load(f) for issuing experience in discord.py, I looked at the guides on how to do it but it doesn’t work out, what’s the problem?

I register the code from sites and guides, but it gives an error when I try to activate this code

File "C:\Hibiki_Bot(!hb)\Hibiki.py", line 217, in on_message
    users = json.load(f)
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)


import discord
import json
from discord import utils
from discord.ext import commands
from discord.ext.commands import Bot
import config
import asyncio
import os
intents = discord.Intents.default()
intents.members = True
bot = discord.Client(intents=intents)
Bot = commands.Bot(command_prefix="!hbk ", intents=intents)
os.chdir(r'C:\Hibiki_Bot(!hb)')</blockquote>


@Bot.event
async def on_message(message):
        with open('usersdata.json', 'r') as f:
                users = json.load(f)
        await update_data(users, message.author)
        await add_exp(users, message.author, 1)
        await level_up(users, message.author, message.channel)

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

async def update_data(users, user):
        if not user.id in users:
                users[user.id] = {}
                users[user.id]['exper'] = 0
                users[user.id]['level'] = 1
async def add_exp(users, user, exp):
        users[user.id]['exper'] += exp
async def level_up(users,user,exp):
        exper = users[user.id]['exper']
        lvl_start = users[user.id]['level']
        lvl_end = int(exper ** (1/4) )
        if lvl_start < lvl_end:
                await Bot.send_message(channel, '{}LvL up to level{}'.format(user.mention, lvl_end))
                users[user.id]['level'] = lvl_end


All error code
Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\HP-pc\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\client.py", line 333, in _run_event
    await coro(*args, **kwargs)
  File "C:\Hibiki_Bot(!hb)\Hibiki.py", line 217, in on_message
    users = json.load(f)
  File "C:\Users\HP-pc\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 296, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "C:\Users\HP-pc\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\HP-pc\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\HP-pc\AppData\Local\Programs\Python\Python37\lib\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)


Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
soremix, 2020-11-12
@Whisky_Hibiki

Invalid JSON in file.

D
Dr. Bacon, 2020-11-12
@bacon

the problem is standard, first we learn python, then we write bots

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question