Discord bot: initialized variables become reset


#1

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?


#2

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


#3

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.


#4

And can I host the database from Glitch.com?


#5

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


#6

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


#7

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


#8

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