Discord bot message filtering not working for specific text patterns

I’m working on a Discord bot that should capture and record messages containing a particular keyword, but it’s not functioning as expected.

bot.on('message', msg => {
    if (!msg.guild) return;
    if (!msg.content.includes('$target')) return;
    if (msg.guild) {
        const logEntry = `[LOG] [${msg.guild.name}] [#${msg.channel.name}] ${msg.author.username}#${msg.author.discriminator}: ${msg.content}\n`;
        bot.channels.get('9876543210987654321').send(logEntry);
        return;
    }
});

The bot should only process messages that have ‘$target’ in them. Right now nothing gets logged even when I send messages with that text. Has anyone dealt with this before?

Double check your bot token permissions and Discord.js version. I hit similar issues migrating from v12 to v14 - the event structure changed a lot. Switch from the deprecated ‘message’ event to ‘messageCreate’ if you’re on a newer version. Your channel retrieval method needs updating like Oscar said, but here’s another gotcha - if the target channel’s in a different guild than where the message came from, you’ll need to fetch it differently. I spent hours debugging what was just a version mismatch where my event listeners weren’t firing. Drop a console.log at the start of your event handler to see if it’s the event firing or your logic that’s broken.

definitely check your bot’s intents in the dev portal. if you haven’t enabled message content intent, it won’t read the message text correctly. also, make sure the channel ID is correct and the bot has permissions there, otherwise it won’t work.

Others covered the technical fixes, but this whole approach is fragile. You’re stuck dealing with API changes, permission issues, and constant maintenance.

I faced something similar - needed to monitor Discord messages across multiple servers for compliance. Custom bot code turned into a nightmare every time Discord updated their API.

Automation workflows work way better. They handle Discord monitoring without touching bot code. Set triggers for keywords, process messages however you want, send logs anywhere.

Best part? No worrying about API deprecations or permission debugging. Configure once and it runs reliably.

Need to change keywords or add complex filtering later? Update the workflow instead of redeploying code.

Your code has a logic issue. You check if (!msg.guild) return; at the start, then later check if (msg.guild), which is redundant since you already returned if there’s no guild. The real problem is likely that bot.channels.get() is deprecated in newer Discord.js versions. Use bot.channels.cache.get('9876543210987654321') instead. Also, ensure your bot has access to that channel ID and the necessary permissions to send messages. I’ve encountered this issue when my bot tried logging to channels it couldn’t reach. Adding some console logging could help you debug what’s failing - whether it’s finding the target text, accessing the guild, or sending the message.

Had this exact problem last month. Your string matching is probably the issue - includes() is case sensitive, so ‘$TARGET’ or '$target ’ with a space won’t match. Use msg.content.toLowerCase().includes('$target') instead. That redundant guild check doesn’t break anything but makes the code messy. Before you try complex solutions, add console.log(msg.content) right after the guild check to see what messages you’re actually getting. In my case, the bot got messages fine, but users had copied invisible characters from somewhere else. Simple toLowerCase() fixed most of it.