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

443
Visualizações
How do I make my Discord Bot reply a same answer for similar messages? JavaScript

I've been working on a Discord bot. I needed a solution for questions like this: Who are you or who are you.

The above sentences may seem similar but are case sensitive, and I want my bot to reply the same answer to a few similar questions like I showed above. This is What I tried:

import Discord from 'discord.js'
import { Client, Intents } from 'discord.js'

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

client.on("ready", () => {
  console.log("Log In Successful!!")
})

client.on("message", msg => {
  if (msg.content === 'hello', 'hey') {
    msg.reply("hi");
  }
})

client.login(process.env.TOKEN)

But when I run it and write the command, it repeats the reply several times.

Any ideas to fix this?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The line that is causing the multiple replies is this one:

if (msg.content === 'hello', 'hey'){

Your syntax is slightly off and the 'hey' is not part of the statement like you want it to be. The 'hey' evaluates to true which triggers the if statement's code a second time.

To fix it, you may want to have an array of responses, and check if the user's message is in the array (note the .toLowerCase() means you don't have to worry about capital letters):

greetings = ['hello', 'hey', 'hi']
if (greentings.indexOf(msg.content.toLowerCase()) > -1) {
    // the message is in the array - run your code here
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You can group similar questions in an array and use Array#includes to check if the question is in that array. You can also make it case-insensitive by using toLowerCase. Something like this:

client.on("message", (msg) => {
  const group = [
    'how are you',
    'how you doin',
    'you alright',
  ]
  if (group.includes(msg.content.toLowerCase())) {
    msg.reply('hi');
  }
})
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