IHZAQ
IHZAQ

Reputation: 102

Avoiding respond to a bots

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?

Upvotes: 2

Views: 75

Answers (1)

MegaMix_Craft
MegaMix_Craft

Reputation: 2210

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

Upvotes: 2

Related Questions