Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discord.js bot replies to itself

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?

like image 930
gitgudgithub Avatar asked Sep 11 '17 13:09

gitgudgithub


People also ask

How do you make a discord bot not respond to itself?

on('message', async msg => { // This block will prevent the bot from responding to itself and other bots if(msg. author. bot) { return } // Check if the message starts with '!

Does autocode use discord JS?

Because of the way Autocode automatically sets up Discord webhooks for you, each event our bot responds to will have a corresponding JavaScript file in your Autocode project: just like functions/events/discord/command. js .

Can I make a discord bot with JavaScript?

Admins can add unique Discord bots to their servers. There are also powerful APIs for creating Discord bots for those who prefer to take matters into their own hands. For instance, Discord. js allows us to create a simple Discord bot using Javascript.


1 Answers

// 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
}
like image 152
yaas Avatar answered Sep 26 '22 08:09

yaas