wild_flamin_pie
wild_flamin_pie

Reputation: 21

how can i stop a discord bot from replying to itself or other bots? (discord.js)

i'm a newbie in this whole coding thing. i started working on my first discord bot a couple of days ago, you know, for me and my friends to mess around with. now, let's say that i want this bot to detect words in a message and reply every time someone mentions that word, no matter in which part of the message. i was able to do this, but now there's a problem. let's say that the word I'm looking for is "hello". if someone says "oh hello", "hello there", a message with the word hello, the bot will reply "hello" back. but the bot will also detect the hello in its own message and reply to itself over and over until i shut it down. here's the code:

bot.on("message", message => {
    const hello = ["hello"];
    if( hello.some(word => message.content.includes(word)) ) {
        message.channel.send("Hello!");
}} ) 

so, i can't figure out how to make the bot not see that "hello" in its own message, or any bot's message if that's easier, but be able to analyze the "hello" from a user, so that it isn't stuck in an infinite loop of replying to itself. how can i do this?? thank you in advance (:

Upvotes: 2

Views: 1321

Answers (2)

Justin
Justin

Reputation: 490

If want to prevent the bot from replying to itself:

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

You can also prevent a bot from replying to another bot:

if (message.author.bot)
   return;

Upvotes: 4

Syntle
Syntle

Reputation: 5174

Just check if the message author is a bot using message.author.bot

bot.on("message", message => {
    if (message.author.bot) return
    const hello = ["hello"];
    if( hello.some(word => message.content.includes(word)) ) {
        message.channel.send("Hello!");
}
})

Upvotes: 2

Related Questions