Welcome to Qi-U Community for programmer and developer-Open, Learning and Share

Categories

0 votes
327 views
in Technique[技术] by (31.9m points)

JSON in python discord.py

I'm learning to code discord bots and I'm trying to add a 'money' function that will add 'money' to the user 'account' every time he sends a message. Here's my problem, the json file writes 'id' : 0 but the python file write id : 0

from discord.ext import commands
import json

with open('money.json', 'r') as infile:
    argent = json.load(money)
    print(money)

@bot.event
async def on_message(message):
    if not message.author.bot:
        id = message.author.id
        if id not in money:
            argent[id] = 0.00
        argent[id] += 5.00
        print(money)
        with open('money.json', 'w+') as outfile:
            json.dump(argent, outfile)

Here's my problem, when python load the JSON file, it creates python dict('id' : value) but when python write in the dict, it writes dict(id : value)

dict loaded from JSON:

{'299561981311057920': 5.0}

Same dict after the program wrote on it :

{'299561981311057920': 5.0, 299561981311057920: 5.0}

I would like it to write:

{'299561981311057920': 10.0}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (31.9m points)

Convert your id to a string before pulling it from your dict

id = str(message.author.id)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Qi-U Community for programmer and developer-Open, Learning and Share
...