Search code examples
node.jsdiscord.js

Disccord.js message.content.includes creates an infinte loop


bot.on('message', async message => {
    if(message.content.includes('stuff')) {
        message.channel.send("*stuff*");
    }
}

So if someone says something with stuff on my server, the bot will infinitely message the chat with "repeat." I literally have no idea why this is happening.


Solution

  • As other commenters have mentioned, you're creating an infinite loop because you're checking your own bot's message too, which sends the same thing you're checking for. Ignoring the bot client or just all bots in general will work:

    bot.on('message', async message => {
        // To ignore your own client:
        if (message.author.id === bot.user.id) {
            return;
        }
    
        // To ignore all bots (including your own):
        if (message.author.bot) {
            return;
        }
    
        if(message.content.includes('stuff')) {
            message.channel.send("*stuff*");
        }
    }