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

158
Vistas
Can msg.content be equal to a variable?

I'm new to writing discord.js and first I don't know if this is a stupid question, but the question is I want to make a bot so that the first section is greeting:

//greetings
const random_greeting = () =>{
    return Math.floor(Math.random() * 5);
  }
  
  client.on('messageCreate', msg=>{
    if(msg.author == client.user) return;
    let greeting = ['Hi', 'Yo', 'Hello', '👋👋', 'Ok...',]
    if (msg.content === 'hi'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'hello'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'yo'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'sup'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'wassup'){
      msg.reply(greeting[random_greeting()])}
    if (msg.content === 'yo'){
      msg.reply(greeting[random_greeting()])}
  })

As you can see i need to write:

if (msg.content === 'hello'){
      msg.reply(greeting[random_greeting()])}

and keep spamming it like 6 times, so my question is there any way to do this short or faster?

I have tried something like adding greetinganswer variable:

let greetingAns = ['hi', 'hello']

but when i used

const random_greeting = () =>{
    return Math.floor(Math.random() * 5);
  }

  client.on('messageCreate', msg=>{
    if(msg.author == client.user) return;
    let greeting = ['Hi', 'Yo', 'Hello', '👋👋', 'Ok...',]
    let greetingAns = ['hi', 'hello']
    if (msg.content === greetingAns()){
      msg.reply(greeting[random_greeting()])}

I think it should work but it doesn't. So is msg.content === a variable?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes, you can do that and you will need to use an array of strings as you already did. So to check if the message content is found in the array it will be needed the includes() javascript method.

Like this:

if (greetingAns.includes(msg.content)) {
    msg.reply(greeting[random_greeting()]);
}
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