• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to randomise a response in discord.js

Zyphon_22

New Coder
Im making a discord bot currently and i was wondering on how to randomise messages on discord.js

the normal way

e.x.

random:
const reply = ["reply1", "reply2"]
const choose_reply = [Math.floor(Math.random() * reply.length)];
console.log(choose_reply);

this works in the console but it doesnt work when i run the bot

is there another way to randomise a message?
 

Malcolm

Administrator
Administrator
Staff Team
Hi there,

I'm in no way a JS expert but what if you were to replace console.log(choose_reply); with client.message.send(author, choose_reply);. But give it a try and let me know if it works or if it doesn't I can continue to look into it.
 

Zyphon_22

New Coder
Hi there,

I'm in no way a JS expert but what if you were to replace console.log(choose_reply); with client.message.send(author, choose_reply);. But give it a try and let me know if it works or if it doesn't I can continue to look into it.
I have tested it out and i had to change it to msg.channel.send() but it did not work instead it only prints out the same reply unless i restart the bot
 

Ghost

Active Coder
I think the issue may be that you are choosing a random reply only once per time the script runs?
You would want to make sure that you choose a random message every time the bot needs to send a message. Based on what you said it sounds like it's only choosing a random reply once, and then the bot is sending that same message each time. The random choice needs to be *in* the loop that the bot's messaging is in, or *in* the function that it's in.

For example, this would be wrong because it chooses a random message just once, then sends that same reply out 3x.
Code:
var reply = ["reply1", "reply2"]
var choose_reply = [Math.floor(Math.random() * reply.length)];
for(var x = 0; x < 3; x++){
    console.log(choose_reply);
}

This would be correct, for a random message each time:
Code:
for(var x = 0; x < 3; x++){
     var reply = ["reply1", "reply2"]
     var choose_reply = [Math.floor(Math.random() * reply.length)];
    console.log(choose_reply);
}
Do you see the difference? This is most likely the issue which would explain why re-running the bot allows another random choice to be chosen, but keeping the bot script open has the same reply each time.

Your code's randomization appears to work fine in console. Of course, since you only have 2 possible replies you will notice a lot of duplicates.
In random trials you might get the same reply 2-4 times in a row.
 

Top