Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Random message using node.js

so i want to let my bot choose from multiple messages when someone sends prefix + random but everytime i test it it says "0" and i dont know why. This is what i have already.

client.on('message', message =>{
  const member = message.guild.member
  if(message.content === prefix + 'random'){
    message.channel.send('random1' | 'random2' | 'random3')
  }

});

i also have tried doing "," instead of "|" but then it only sends random1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to send a random string, you cannot use OR operator (which is || btw…).

You have to do something like:

const randomStrings = ['random1', 'random2', 'random3'];
const getRandomInt = (max) => Math.floor(Math.random() * max);

client.on('message', (message) => {
  const member = message.guild.member;
  if (message.content === prefix + 'random') {
    message.channel.send(randomStrings[getRandomInt(randomStrings.length)]);
  }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading