Discord.js | Random messages

var testmessages = ["message one", "message two", "message three", "message four"]
var testrandomform = testmessages[Math.floor(Math.random()*testmessages.length)];

module.exports = {
    name: 'random',
    description: "",
    execute(message, args){
        message.channel.send(testrandomform).then().catch(console.error);
    }
}

So, the problem with this is so far although it does load and send messages. It only picks the first word of testmessages and never picks the other ones. If possible I would like some help on this, I’m sorta new to this and would like some minor help if anyone can offer it?

The code is stored in the command name file called random.js which is loaded from main.js
I have tried and did find some posts related to this but those ended with zero finds as well.
The reason for the needing of random messages is because I would like a randomized message each time the user activates the command. For an example, if the user activates the command !help I will make it where it sends an embed to the user via message.author but here is the other thing. I would like it to also send a message to the channel via message.channel but I want this to be randomized.

1 Like

Hello, welcome to the GitHub Support Community! The reason you’re getting the same response each time is because your testrandomform variable is only being randomize upon startup, not each time a command is parsed and executed. There’s one similar topic on this forum that I gave a similar solution to, but I will provide one here as well. This should fix your issue:

var testmessages = ["message one", "message two", "message three", "message four"]

module.exports = {
    name: 'random',
    description: "",
    execute(message, args){
        var testrandomform = testmessages[Math.floor(Math.random()*testmessages.length)];
        message.channel.send(testrandomform).then().catch(console.error);
    }
}

Hope this fixes your problem!