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

178
Vistas
how to pick a random string from an array every time the bot answers

I'm trying to develop a simple discord bot which's made for chatting. I've created a string array that's supposed to store all answers the bot can give and then let the program give a random response based on that array. Now the bot does give a random answer but he only picks one random one from the array and only changes it when he's restarted. I was wondering if there's a way to make the bot pick a different answer every time he says something. Here's the current code:

const stringarray = [
      'string1',
      'string2',
      'string3',
];

let randomNumber = Math.floor(Math.random()*stringarray.length);

client.on("message", msg => {
  if (msg.content === "string0") {
    msg.reply(stringarray[randomNumber]);
  }
})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem is you are setting randomNumber in the main code and not in the .on('message') callback! Therefore, the code is executed when you launch the server (and thus only once). You need to move it inside the callback for it to compute a new random value for each call:

client.on("message", msg => {
  if (msg.content === "string0") {
    let randomNumber = Math.floor(Math.random()*stringarray.length);
    msg.reply(stringarray[randomNumber]);
  }
})
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