I have a $count command which increases a variable “count” by 1. Every time I reset my bot, the count goes back to 0, because I set it with let count = 0. My issue is that I don’t want the variable to reset, but removing the line just leaves count undefined. How can I use MongoDB (mongoose) to store the variables previous data through resets?
My Code:
let count = 0
client.on('messageCreate', (message) => {
if (message.content === '$count') {
count = count + 1;
message.reply(`The count is now ${count}.`);
}
});
I tried deleting the let statement, but the variable is undefined. Is there something I can set the variable to in order to keep it the same since before the restart?
Edit:
I tried creating a schema to store the variable, but it prints as “NaN.” I’m not sure why it isn’t a number, as I have set the “number” property on the variable.
const { Schema, model } = require('mongoose');
const globalSchema = new Schema({
count: {
type: Number,
default: 0
},
},
);
module.exports = model('Global', globalSchema)
In the other file:
const Global = require('../src/schemas/global');
client.on('messageCreate', (message) => {
if (message.content === '$count') {
Global.count = Global.count + 1;
message.reply(`The count is now ${Global.count}.`);
}
});