我检查用户是否在表中的代码:
@client.event
async def on_message(ctx):
id = ctx.author.id
with open('coins.json') as coins:
coinData = json.load(coins)
with open('shop.json') as shop:
shopData = json.load(shop)
await client.process_commands(ctx)
if id in coinData:
print('exists')
else:
coinData["players"][id] = 0
with open('coins.json', 'w') as coins:
json.dump(coinData, coins)
它正在读取的 JSON 文件:
{"players": {"325616103143505932": 0}}
当有人发送消息时会发生什么:
{"players": {"325616103143505932": 0, "325616103143505932": 0}}
而且它不会在控制台中打印“存在”,无论该人发送了多少消息,但它只添加了两次键值对。
幕布斯7119047
相关分类