How can I make a variable "refresh" itself every time my JavaScript file has code pulled from it?

First, let me apologize if the title is a bit specific, I really don't know how to phrase my question without being vague. Okay, so I'm working on a Discord Bot. I'm trying to allow the bot to send a message in response to a command containing one of the strings within a pre-defined array. In my case, the bot picks a random rap lyric from the array and says it. The problem? The "randomizer" code that determines which string from the array is used only runs once I activate the bot. Here's the code:

var barnumber = Math.floor(Math.random() * dababy_bar.length);

module.exports = {
    name: 'flamebar', 
    description: "spits out a flame bar",
    execute(message, args){
        message.channel.send(dababy_bar[barnumber]);
    }

So, my issue is that whenever the Discord Bot sends out a message in response to the command, it only uses the code that's contained within module.exports, and module.exports requires a specific format, so sneaking in a function isn't do-able. This means that the "randomizer" that determines which string is used doesn't run every time the command is called. I've already tried sticking the "randomizer" at the end of the code, after module.exports, but that didn't have any changes in the results. I then tried to loop the "randomizer" forever, but that didn't work as the console froze whenever I started the bot up. Does anyone have any tricks I could try to harness to get around this? Thanks in advance!



Read more here: https://stackoverflow.com/questions/66995708/how-can-i-make-a-variable-refresh-itself-every-time-my-javascript-file-has-cod

Content Attribution

This content was originally published by chefanthonyy at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: