0

I've been trying to put a random reply but the other answers and tutorials I found didn't exactly work. Most of the tutorials have

const messages = [
    "seriously?! You thought I would reply", 
    "hm, yeh thats a pretty random question - Don't ya think?", 
    "Ok I'm actually running out of options now", 
    "Please stop asking me", 
    "Ok, im done!",
    "⛔"
];

const randomMessage = messages[Math.floor(Math.random() * messages.length)];

And to complete it and send the message

module.exports = {
    name: 'random',
    description: 'random?',

    execute(message, args){
        message.channel.send(randomMessage);
    }
}

However, the answer isn't really randomized. When I run the bot from the command prompt/terminal, the bot gets a random answer, but then when the user actually runs it, it only gives one answer.

For example, the answers can be 1, 2, or 3. When I run the bot one answer gets picked randomly; let's say 2. Then no matter what all users say, it will only give the answer of 2 as the reply. If I run the bot again and get, let's say 3, then the reply will only be 3.

Lauren Yim
  • 12,700
  • 2
  • 32
  • 59
  • 3
    please provide more context - whats the error message, or does nothing happen? Is execute even being called? Where is execute being called? – Ronak Shah Sep 28 '20 at 07:47
  • Have you attempted it? Where's your code? What errors do you get? – Elitezen Sep 28 '20 at 11:40
  • Oh sorry :/ It's my first post. What's happening is that the answer isn't really randomized. What happens is when I run the bot from the command prompt/terminal, the bot gets a random answer, but then when the user actually runs it, it only gives one answer. say the answers can be 1, 2, or 3. You run the bot and then one answer gets picked randomly; Let's say 2. Then no matter what all users say, it will only give the answer of 2 as reply. If I run the bot again and get, let's say 3. Then the reply will only be 3. – TheUnknown1050 Sep 28 '20 at 13:14
  • 1
    ```randomMessage``` constant should be inside of the execute function, otherwise, it will execute only once when the bot is launched. – Gabriel Andrade Sep 28 '20 at 14:07
  • Hi, welcome to Stack Overflow! Next time, please [edit] your post if you need to add more information about your problem instead of commenting it. – Lauren Yim Sep 29 '20 at 01:48
  • So sorry about that, but thanks for the answers guys – TheUnknown1050 Sep 29 '20 at 06:06

1 Answers1

1

As Gabriel Andrade said, you need to put the randomMessage constant inside of the execute function. If you don't, the random message will only be evaluated once when you start the bot.

module.exports = {
    name: 'random',
    description: 'random?',
    execute(message, args){
        // Now the randomMessage will be recalculated every time the command is run
        const randomMessage = messages[Math.floor(Math.random() * messages.length)];
        message.channel.send(randomMessage);
    }
}
Lauren Yim
  • 12,700
  • 2
  • 32
  • 59