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

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

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 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