Home > Back-end >  ##json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)##
##json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)##

Time:08-08

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

enter image description here

I created a discord bot for chat management, I entered that all user data (WARNS, CAPS) should be entered in the .json file, but it doesn't open the file to enter the .json data in the temporary variable, I forgot forum, but I did not find a similar problem and the solution to this problem This is my code:

    import discord
    from Token import Token
    import sys
    import os
    import json
    import time
    from discord.ext import commands
    from discord.utils import get
    client= discord.Client()
    BADWORDS = ["fuck", "suck", "dum", "looser", "losser"]
    LINKS = ["http", "https", "://", ".ru", ".com",".net", ".org", ".biz", ".gg"]
    
    if not os.path.exists('users.json'):
        with open("users.json","w") as file:
            file.write("{}\n".format(str))
            file.close()
    
    @client.event
    async def on_ready():
        print('We are loged as {0.user}'.format(client))
        for guild in client.guilds:
            for member in guild.members:    
                with open('users.json','r') as file :
                    data = json.load(file)
                    file.close()
                with open("users.json","w") as file :
                    data[str(member.id)] = {
                        "WARNS": 0,
                        "CAPS": 0
                    },
    
                    json.dump(data, file, indent=4)
                    file.close()
    @client.event
    async def on_message(message):
        WARN = BADWORDS   LINKS
      
        for i in range(0, len(WARN)):
            if WARN[i] in message.content.lower():
                await message.delete()
                with open("users.json","r") as file:
                    data = json.load()
                    file.close()
      
                with open("users.json","w") as file:
                    data[str(message.author.id)]["WARNS"]  = 1
                    json.dump(data, file, indent=4)
                    file.close()
                emb = discord.Embed(
                    title="Rules Ignored",
                    description=f"*Rcent he have {data[str(message.author.id)]['WARNS'] - 1} rulles ignored, if he continued 7 times he will have BAN!",
                    timestamp=message.created_at
                    )
                emb.add_field(name = "Chanel:", value= message.chanel.mention, inline=True)
                emb.add_field(name = "User:", value=message.author.mention, inline=True)
                emb.add_field(name = "Type of Rules: ", value="Badword/Lincks", inline=True)
      
                await get(message.guild.text_chanels, id=1004729057277120624).send(embed=emb)
      
                if data[str(message.author.id)]['WARNS'] >= 7:
                    await message.author.ban(reason= "You was banned becouse you Ignored Rules 7 times")
        
            if message.content.isupper():
                with open("users.json","r") as file:
                    data = json.load(file)
                    file.close()
      
                with open('users.json','w') as file:
                    data[str(message.author.id)]["CAPS"]  = 1
                    json.dump(data, file, indent=4)
                    file.close()
      
                if data[str(message.author.id)]["CAPS"] >= 3:
                    with open("users.json","w") as file:
                        data[str(message.author.id)]["CAPS"] = 0
                        data[str(message.author.id)]["WARNS"] = 1
                        json.dump(data, file, indent=4)
                        file.close()
        
                    emb = discord.Embed(
                        title="Rules Ignored",
                        description=f"*in the past he have {data[str(message.author.id)]['WARNS'] - 1} rulles ignored, if he continued 7 times he will have BAN!",
                        timestamp=message.created_at
                        )
                    emb.add_field(name = "Chanel:", value= message.chanel.mention, inline=True)
                    emb.add_field(name = "User:", value=message.author.mention, inline=True)
                    emb.add_field(name = "Type of Rules: ", value="CAPS", inline=True)
      
                    await get(message.guild.text_chanels, id=1004729057277120624).send(embed=emb)
      
    
    client.run(Token.token)

CodePudding user response:

I think you need to place curly brackets in the json file like this.

{}

You don't need to fill these brackets. Just put these in the your file and then try again.

CodePudding user response:

You put .format(str)

   if not os.path.exists('users.json'):
        with open("users.json","w") as file:
            file.write("{}\n".format(str))
            file.close()

This writes: <class 'str'> in the json file

This wont work, if you checked the json file you would've seen. Just remove the .format(str) part and it should work.

  • Related