1

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
}
  • 1
    Are you sure that is the content of the file the bot writes into, `cup.json`? – Filip Müller Aug 11 '22 at 15:14
  • yes I am, at least as far as I can see/understand it is. – whenimbender Aug 11 '22 at 15:25
  • Aren't you maybe looking at a cached version? Did you try closing and reopening the file? I don't see anything that would cause this in the code. But it's pretty long code, try providing a [mre], perhaps the problem will clarify. – Filip Müller Aug 11 '22 at 15:37
  • 2
    I just figured it out. The code does work as intended, it is a problem with how my host (Heroku) operates. I will close this as there wont be anything I can do until I find a new hosting situation. Thank you for the help.; – whenimbender Aug 11 '22 at 15:47
  • @whenimbender You can answer it yourself and accept it that this question has a good answer – puncher Aug 11 '22 at 16:24
  • Does this answer your question? [How to create permanent files on Heroku?](https://stackoverflow.com/questions/64258955/how-to-create-permanent-files-on-heroku) – Gino Mempin Aug 16 '22 at 09:29

1 Answers1

1

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • "..it is a problem with how my host (Heroku) operates". Well, not exactly Heroku's problem. It is _by-design_ that [their file system is ephemeral](https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem). Also see https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted-from-the-application. – Gino Mempin Aug 16 '22 at 09:33
  • Yes, you are right. But it is still a problem in my eyes as it makes my bot non-functional. The solution to the problem is migrating elsewhere or storing data externally, which is what I am doing. – whenimbender Aug 16 '22 at 19:28