Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Having an issue with randomization in Discord JS

so i've been trying to make it so that every time the user sends a message there's a 50/50 chance that the bot reacts with an emoji, however the number is only picked every time i restart the bot. any ideas on how i can make it pick the number every time the command is triggered?

i tried to use setInterval but that didn't work so idk

here code;

const userID = "user id"
let random = Math.floor(Math.random()*2)

client.on('messageCreate', (message) => {
  if(random === 1) {
    if(message.author.id === userID) {
      message.react('emoji');
    }
  }
});
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Simply, define the random variable inside the messageCreate event.

Example:

const userID = "user id";

client.on('messageCreate', (message) => {
  let random = Math.floor(Math.random() * 2);
  if (random === 1) {
    if (message.author.id === userID) {
      message.react('emoji');
    }
  }
});
about 4 years ago · Juan Pablo Isaza Report

0

If you define random inside of the messageCreate event it means that a new random number is generated each time the even is called.

const userID = "user id"

client.on('messageCreate', (message) => {
  let random = Math.floor(Math.random()*2)
  if(random === 1) {
    if(message.author.id === userID) {
      message.react('emoji');
    }
  }
});

As Tyler2P has already shown an example, just wanted to explain why this works. Thanks

about 4 years ago · Juan Pablo Isaza Report

0

You could use a Math.random 50/50 chance, like this:

const userID = "user id"

client.on('messageCreate', (message) => {
    if (Math.random() >= 0.5) {
        if (message.author.id === userID) {
            message.react('emoji');
        }
    }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!