Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda