discord.js bot replies to itself

18.9k Views Asked by At

I am currently coding my first discord bot, it can already play YouTube music.

if (message.content.includes("Good Job") || 
    message.content.includes("good job")) {
    message.channel.sendMessage("Good Job everyone :smirk:");
}

As you see, if someone types "good job" (this is just an example) then the bot will reply with "good job everyone :smirk:), but then the spam will begin: the bot reads his own message and replies to it.

How can I prevent the bot from answering itself?

6

There are 6 best solutions below

3
Jörmungandr On

Use this in the on message event:

if (message.author.bot) return;

for more info: https://anidiotsguide.gitbooks.io/discord-js-bot-guide/coding-guides/a-basic-command-handler.html

0
TheThirdDimension On

The reason why your bot replies to yourself is because where you put:

if (message.content.includes("Good Job") || 
    message.content.includes("good job"))

It is basically checking the chat if a piece of text includes the words "Good Job" or "good job". As your bot sends:

message.channel.sendMessage("Good Job everyone :smirk:");

as an answer, it creates a loop because that message includes the words "Good Job", basically running the code again and again and again.

Of course, the easiest way of fixing this is for you to change the answer it gives to not include the words Good Job, but there is better solution to make sure it doesn't happen for all of the commands you might make.

As @Jörmungandr said, under the message event include:

if (message.author.bot) return;

to make sure it doesn't happen. It essentially checks if the author of the message is a bot and if it is, it ignores it.

0
Pruina Tempestatis On

You can simply just check if the user sending the message is a bot. For example:

if (!msg.author.bot) {
    <Your code to execute if the user is not a bot.>
} 

Hope this was helpful, thank you!

0
yaas On
// In message event
if(message.author.id === client.user.id) return;

// I would recommend a variable like this for splits on words
// const args = message.content.trim().split(/\s+/g); 
// You could also .slice() off a prefix if you have one

if(/good job/i.test(message.content)) {
  message.channel.send('Good job everyone :smirk:'); // sendMessage is deprecated, use send instead
}
0
BotNC On

You may use this code which avoids doing anything if the author is a bot:

if(message.author.bot) return;
0
Aawesome On

There are quite a lot of ways to solve this.

1: Stopping the Bot from Responding to ANY bot.

Right under your message callback (the client.once('message')), you can just add:

if (message.author.bot) return;

2: Stopping the Bot from Responding to itself

Once again, right under your message callback, you just add:

if (message.author.id == client.user.id) return;

Please note the client that I am referring to is your bot's client, created when you type

const client = new Discord.Client()

...or whatever your client is. It's basically the thing that you login to (ex: client.login(yourtokenhere))

Most tutorials will call this Client, client, or maybe even bot. The discord.js guide will call it a Client.