Home > Back-end >  Adding data to json file when command is ran discord.py
Adding data to json file when command is ran discord.py

Time:10-14

Need some assistance I would like it to add server data again everytime the command is ran The goal would be to eventually change it to on_guild_join So everytime it joins a server it creates a like server setup Guild id, name, and like data storage for other parts of the bot to access

Error:

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

Code:

 with open("./serverconfigs.json") as f:
        data = json.load(f)
    
    newserver = configData["Servers"]
    
    
@client.event
async def on_ready():
    print(client.user.name)
    print(client.user.id)
    print('Client ready!')


@client.command()
async def test1(ctx):
    with open('serverconfigs.json', 'r ') as f:
        serverdata = {
            "guildid":"",
            "guildname":"",
            "bannedwords":[],
            "blacklistedchannels":[],
            "modrole":[]
        }
        serverdata.append(newserver)
        data = json.load(f)
        json.dump(data, f, indent=2)
    await ctx.send('Data Collected.')

CodePudding user response:

@client.command()
async def test1(ctx, channel: discord.TextChannel, role: discord.Role, *, bannedwords: str):
    with open('serverconfigs.json') as json_file:
    data = json.load(json_file)

        serverdata = {
            "guildid":guild.id,
            "guildname":guild.name,
            "bannedwords":bannedwords,
            "blacklistedchannels":channel,
            "modrole":role
        }
        data.append(serverdata)

        with open('serverconfigs.json','w') as j:

        json.dump(data,j,indent=2)

    await ctx.send('Data Collected.')

I haven’t had time to test this but it should work let me know if there is anything wrong with it!

  • Related