Home > Enterprise >  Avoiding respond to a bots
Avoiding respond to a bots

Time:11-07

I'm working on discord bot and making ping command as test, but it loops because it responds to itself. However, if I put message.author.bot === false in every command my code will be a mess!

client.on("message", message => {
  if(message.content === "ping" && message.author.bot === false){
    message.channel.send("pong")
  } else if(message.content === "pong" && message.author.bot === false){
    message.channel.send("ping")
  }
})

Is there a better way to solve this?

CodePudding user response:

You need to put if(message.author.bot) return; after client.on("message", message => { and as result get:

client.on("message", message => {
  if(message.author.bot) return;
  if(message.content === "ping" && message.author.bot === false){
    message.channel.send("pong")
  } else if(message.content === "pong" && message.author.bot === false){
    message.channel.send("ping")
  }
})

to ignore messages from bots

  • Related