Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to save user id only once in json file discord.py

The code I have saves the same user in the json file again:

@client.command()
async def Shibaku1(ctx, coin1, coin2, coin3, coin4, coin5, coin6):
    with open('Shibaku1.json', 'r') as f:
      coins_data = json.load(f)
    coins_data[ctx.author.id] = (coin1, coin2, coin3, coin4, coin5, coin6)
    with open('Shibaku1.json', 'w') as f:
      json.dump(coins_data, f)

I tried making an if statement in order to not happen, but it didn’t seem to work

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Python automatically updates keys in dictionary if they exist.

You should convert ctx.author.id to str using str(ctx.author.id):

@client.command()
async def Shibaku1(ctx, coin1, coin2, coin3, coin4, coin5, coin6):
    with open('Shibaku1.json', 'r') as f:
      coins_data = json.load(f)
    coins_data[str(ctx.author.id)] = (coin1, coin2, coin3, coin4, coin5, coin6)
    with open('Shibaku1.json', 'w') as f:
      json.dump(coins_data, f)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading