user15494703
user15494703

Reputation:

How to make sure that Discord.JS bot doesn't erase all objects when restarted?

So basically, let's say I had a list like:

let optIn = {}

and I had some code like:

let optIn = {}

const commands = {
    optIn: PREFIX + "optIn"
}

bot.on("message", msg => {
    if (msg.content === commands.optIn) {
        optIn[msg.guild.id] = msg.author.id
        msg.channel.send("**WARNING** You are now going to receive messages when someone makes a new invite.")
    }
})

If the bot restarts, the optIn variable would be set to {}/null so then how would I make it save that guild id to that variable, even if the bot restarts?

Also, if you need the inviteCreate event code, here it is:

bot.on("inviteCreate", event => {
    if(!optIn[event.guild.id]) {
        event.guild.systemChannel.send("Looks like an invite was made but the server has no user to send invite alerts to. If you want to opt in, send `amඞgus:optIn` to any channel in the server.")
    } else {
        bot.users.fetch(optIn[event.guild.id]).then(r => {
            r.send("**INVITE MADE**\nAn invite was made by <@!" + event.inviter.id + ">, in a server called `" + event.guild.name + "` and the Invite URL is `" + event.url + "`.")
        })
    }
})

Upvotes: 1

Views: 100

Answers (2)

user15494703
user15494703

Reputation:

As @Sporego suggested, I actually am writing to a .json file instead, works even better and it's free.

Upvotes: 0

Sporego
Sporego

Reputation: 416

To solve this you need to connect your node.js application to a persistent database. (Your deployment probably wipes it's data on restart and redeployment)

Try Firebase? (Might be easiest) maybe MongoDB w/ Mongoose?

You probably can't store this in local memory, unless you deployed this on a VPS or something along those lines!

Upvotes: 0

Related Questions