Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda