簡體   English   中英

JSON 在 python discord.py

[英]JSON in python discord.py

我正在學習對 discord 機器人進行編碼,我正在嘗試添加一個“金錢”function,每次他發送消息時都會向用戶“帳戶”添加“金錢”。 這是我的問題,json 文件寫入 'id': 0 但 python 文件寫入 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)

從 JSON 加載的字典:

{'299561981311057920': 5.0}

程序在其上寫入后的相同字典:

{'299561981311057920': 5.0, 299561981311057920: 5.0}

我希望它寫成:

{'299561981311057920': 10.0}

將您的 id 轉換為字符串,然后再從您的 dict 中提取它

id = str(message.author.id)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM