0

我一直试图让这段代码工作一段时间;

@bot.group()
async def donator(ctx):
    '''Commands for donators'''

@donator.command()
async def register(ctx, Steam_ID : str):
    '''Registers your donator status'''
    if SteamID(Steam_ID).is_valid():
        convert = SteamID(Steam_ID)
        Steam64ID = convert.as_64

        author = ctx.author

        with open('donators.json') as d:
            data = json.load(d)

        newstring = {
        f"{author.id}": {
                "steam": f"{Steam64ID}",
                "tier": "6"
            }
        }

        data.update(newstring)

        with open('donators.json', 'w') as d:
            json.dump(data, d, indent=2)

    else:
        ctx.send("Invalid Steam ID!\nThis command accepts any kind of Steam ID.")

当我运行它时,它会产生我想要的结果,写入一个具有正确信息的有效 json 文件,没有错误。如果我再次运行它,没有任何反应,绝对没有。这就是 json 最初的结构。

{
  "000000000000000000": {
    "steam": "00000000000000000",
    "tier": "0"
  }
}

运行 python 脚本后,它看起来像这样

{
  "000000000000000000": {
    "steam": "00000000000000000",
    "tier": "0"
  },
  "240912491624923137": {
    "steam": "76561197960265729",
    "tier": "6"
  }
}

如果我第二次运行它,什么也不会发生。没有写入任何内容,json 文件保持不变并且没有错误。

4

1 回答 1

0

您应该将 JSON 文件构建为字典列表。

{"data": [{"id": 0, "name": "Hello"}, {"id": 1, "name": "Test"}]}
于 2020-09-07T22:49:18.960 回答