Reputation: 21
I have a json load/save/dump function to count how many time a single word is said in a specific channel. It works great, but I lose the data after reboot of the bot. Below is my code.
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.")
Here is the json file - right now if I were to run !totalcup, the bot spits out '13' but the file says 0. Not sure if I am missing something as I am new to code.
{
"cup": 0
}
Upvotes: 1
Views: 108
Reputation: 21
I just figured it out. The code does work as intended, it is a problem with how my host (Heroku) operates. There isn't anything I can do until I find a new hosting situation.
Upvotes: 1