Discord bot: initialized variables become reset


Hello, so my bot has an empty array that has items pushed into it and pushed out a few hours later. Before certain ones can be pushed out however, the array becomes empty again. Here’s an example:

var array = [];
//Values get pushed in
array = ['value', 'value', 'value'];

//What I want a few hours from now:
array = ['value', 'value'];

//What happens:
array = [];

How can I fix this?


I think it’s because you are redefining the variable in var array = [] maybe i am wrong since i cannot see the full code.


A link to your project would help, but note that if your project sleeps then any data stored in variables will be lost. If you’re wanting to store data longer-term you’d be better writing it to a file or database.


And can I host the database from Glitch.com?


Yep, here are some examples: https://glitch.com/storage


Thanks! I’ll get to adding one and see if that works.


But wait, won’t the database go to sleep after 5 minutes and have all of the values reset?


The filesystem on Glitch is persistent meaning the contents of files and databases remains after sleeping.