#🔒 Some problem with JSON in Python Discord Bot

22 messages · Page 1 of 1 (latest)

old pagoda
#

I'm python begineer and I made this code:

async def rejestracja(ctx):
    # pobieranie
    user_id = ctx.author.id
    user_nickname = ctx.author.name  # Użyj normalnego nicku

    # otworzenie
    with open("database.json", "r") as file:
        data = json.load(file)

    # sprawdzanie
    if str(user_id) in data:
        embed = discord.Embed(title="Błąd", description="Użytkownik już istnieje w bazie danych.", color=discord.Color.red())
        await ctx.reply(embed=embed)
    else:
        # dodawanie
        data[str(user_id)] = {"nickname": user_nickname, "value": 0}
        with open("database.json", "w") as file:
            json.dump(data, file, indent=4)
        embed = discord.Embed(title="Rejestracja", description=f"Pomyślnie zarejestrowano użytkownika {user_nickname}!", color=discord.Color.green())
        await ctx.reply(embed=embed)```
This code supposed to register user after usage to JSON and throw error if its already registered. But problem is im getting this error: `2024-07-10T11:54:04.744714496Z  discord.ext.commands.errors.CommandInvokeError: Command raised an exception: JSONDecodeError: Expecting value: line 1 column 1 (char 0)[0m` and i dont know how do i fix it
autumn canyonBOT
#

Hey @old pagoda!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
#

@old pagoda

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

prisma reef
#

Are you sure that database.json is valid JSON?

old pagoda
#

i host it on some hosting

#

online

#

the whole bot

#

with all files

#

i think it detects the file because it would show else it can't find the file or smh like that

#

I think

prisma reef
#

Yes, but whatever it's trying to read isn't valid JSON

old pagoda
prisma reef
old pagoda
#

its actually empty file

#

but like how do I make first registration then

prisma reef
#

You can put {} into the file

#

Or you can try reading the database, catch a FileNotFoundError and in that case just set data to an empty dict; {}

autumn canyonBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.