I have a discord bot that includes user data inside a class, and every new object is in one array that stores all of them. I want to be able to have the same data in the array every single time I run or shut down the bot. It is perfectly fine if I have to send a command to the bot like '/save' before shutting it down. How can I do that?
I am not too familiar with discord bots but in JavaScript, you cannot write to the file system due because of security constraints which means you cannot store it in a file locally. The best way to tackle this issue without using a server to write to its local file system is to use a database. So if your user array is something like this
[
{
username: example,
age : 222,
userId: 1
},
{
username: exampleTwo,
age : 221,
userId: 2
}
]
You can just save each key-value pair into the database in a users table:
| Username | age | userId |
| ---------- | ---- | ------ |
| example | 222 | 1 |
| exampleTwo | 221 | 2 |
This way you can also set ID to autoincrement to allow each user to be unique
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.