I'm making a Discord.js bot and one of the functions of the bot is to return a random item from a Javascript array of facts when a user types "!fact". This question has been asked a lot by other users and I've used code from answers given to them but I've run into one problem: the bot gets "stuck" on one fact and doesn't go through the list randomly every time "!fact" is typed. This is an example of the code I have so far:
var facts = [ "Fact 1", "Fact 2", "Fact 3", "Fact 4" ]
var fact = Math.floor(Math.random() * facts.length);
And then, for the bot to send the message:
client.on('message', message => {
if (message.content === "!fact") {
message.channel.send(facts[fact]);
console.log('Message sent');
}
});
But this would only return something like Fact 1
over and over, no matter how many times "!fact" is typed. How can I make it change every time?