Discord Py Bot-json文件正在被写入,但在重新加载后不会保存



我有一个json加载/保存/转储函数,用来计算一个单词在特定通道中说了多少次。它工作得很好,但我在重启机器人后丢失了数据。下面是我的代码。

def load_counters():
with open('cup.json', 'r') as f: 
counters = json.load(f)
return counters
def save_counters(counters):
with open('cup.json', 'w') as f:
json.dump(counters, f)
if message.channel.id == 709551578612498453:
if message.content == ('cup'):
counters = load_counters()
counters["cup"] += 1
save_counters(counters)
return
else:
cup_meta = client.get_channel(709984510678269982)
cup_channel = client.get_channel(709551578612498453)
await cup_meta.send(message.author.mention + ' has violated the sacred rules of Cup')
await message.delete()
await cup_channel.send('cup')
return
with open('cup.json', 'r') as f:
counters1 = json.load(f) # Open and load the file
totalcup = counters1['cup']
if message.content == ('!totalcup'):
await message.channel.send(f"Cup has been said {totalcup} times since Bender reset me.")

这是json文件-如果我要运行的话,现在就可以了!totalcup,机器人吐出"13",但文件显示为0。不确定我是否遗漏了什么,因为我是新手。

{
"cup": 0
}

我刚刚想明白了。代码确实按预期工作,这是我的主机(Heroku(如何操作的问题。在我找到一个新的托管环境之前,我什么都做不了。

最新更新