我已经试着让这个代码工作一段时间了;
@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文件保持不变,没有错误。
发布于 2020-09-07 22:49:18
您应该将JSON文件构造为字典列表。
{"data": [{"id": 0, "name": "Hello"}, {"id": 1, "name": "Test"}]}https://stackoverflow.com/questions/63785088
复制相似问题