1
votes

I'm using a JSON file to store the number of warnings a user has. When a user is warned, I want to increase the number of warnings.

This is the JSON sample I use :

{
"users": {
    "976797937789198397" : "1",
    "270904126974590976" : "2",
    "871490226692976791" : "1"

}  }

This is the code I use to fetch the number of warnings a user has, and this works without any errors. I just need a way to find out how to add +1 to a user every time they are warned.

@commands.command()
async def getWarns(self, ctx : commands.Context, member : discord.Member):
    with open('warns.json') as f:
        data = json.load(f)
        await ctx.send(data["users"][f"{member.id}"])
1
You just retrieve a value of the outer dictionary which is also a dictionary. Then you get, modify and set a value of the inner dictionary. What is the problem?Michael Butscher
What have you tried? It's impossible to know what specific problem you are facing without this, and the question ends up being a code-writing request which is off-topic for SO. Please see How to Ask for more information.TheFungusAmongUs

1 Answers

3
votes

You can use int() to convert the value of the specific ID to int, then increase it by 1. Something like:

f = {
"users": {
    "976797937789198397" : "1",
    "270904126974590976" : "2",
    "871490226692976791" : "1"

}  }

warn_int = int(f['users']['976797937789198397'])

if userIsWarned:
    warn_int = warn_int +1
    f['users']['976797937789198397'] = str(warn_int)