Discord bot message logging functionality not working with specific text filter

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

bot.on('message', msg => {
    if (!msg.guild) return;
    if (!msg.content.includes('*report')) 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('9876543210123456789').send(logEntry);
        return;
    }
})

The bot should only capture messages that have ‘*report’ in them but nothing gets logged at all. I’ve been stuck on this for a while and can’t figure out what’s wrong. Has anyone encountered this issue before?

first, check if your bot’s actually getting message events - throw a console.log at the start of your listener to see what’s triggering. could be an intent issue where the bot can’t see messages at all.

The Problem:

Your Discord bot isn’t logging reports to the designated channel. Messages containing “report” are not being captured and forwarded. This is likely because you’re using a deprecated method to fetch the channel and/or your bot lacks the necessary permissions or intents.

:thinking: Understanding the “Why” (The Root Cause):

The core issue is a combination of outdated methods and missing permissions/intents. bot.channels.get() is deprecated and might fail silently. Without the necessary permissions (“Send Messages” and possibly “View Channel”) in both the monitored channels and the logging channel, the bot can’t send log entries. Finally, without the MESSAGE_CONTENT intent enabled, your bot won’t receive the message content, preventing the includes() check from working correctly.

:gear: Step-by-Step Guide:

  1. Update Channel Fetching and Implement Error Handling: Replace the outdated bot.channels.get() method with the more reliable bot.channels.cache.get() or bot.channels.fetch(). Use a try...catch block to handle potential errors:

    bot.on('message', async msg => {
        if (!msg.guild) return;
        if (!msg.content.includes('*report')) return;
        try {
            const logChannel = await msg.guild.channels.fetch('YOUR_LOGGING_CHANNEL_ID'); // Replace with your logging channel ID
            if (!logChannel) {
                console.error("Logging channel not found!");
                return;
            }
            const logEntry = `[LOG] [${msg.guild.name}] [#${msg.channel.name}] ${msg.author.username}#${msg.author.discriminator}: ${msg.content}\n`;
            await logChannel.send(logEntry);
        } catch (error) {
            console.error("Error logging message:", error);
        }
    });
    

    Remember to replace "YOUR_LOGGING_CHANNEL_ID" with the actual ID of your logging channel.

  2. Verify Bot Permissions: In your Discord server, ensure your bot has the “Send Messages” permission in the logging channel and the “View Channel” permission in any channels where you want to monitor for reports. Without these, log messages will not be sent.

  3. Enable MESSAGE_CONTENT Intent: Go to the Discord Developer Portal, find your bot’s application, and in the “Privileged Gateway Intents” section, ensure that the MESSAGE_CONTENT intent is enabled. This is essential for your bot to access the message content, which is required for your includes('*report') check.

:mag: Common Pitfalls & What to Check Next:

  • Case Sensitivity: The includes() method is case-sensitive. Ensure “report” in your code matches the case used in the messages.
  • Extra Spaces: Check for extra spaces around “report” in the messages.
  • Multiple Bot Instances: Running multiple instances of your bot could lead to unexpected behavior.
  • Rate Limits: Discord rate limits might be causing problems. Monitor the number of messages your bot is sending.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

The problem’s probably deeper than just the deprecated method. I hit something similar where my bot wasn’t getting message events right. Check if you’ve got MESSAGE_CONTENT_INTENT enabled in the Discord Developer Portal - that became required for reading message content and causes exactly this silent failure. Without it, your bot gets the event but msg.content comes back empty, so your includes() check never works. Also make sure your bot’s actually in the server with proper permissions. Throw a console.log right after the message event fires to see if it’s even hitting your code.

The Problem:

Your Discord bot is not logging messages containing the keyword ‘report’, even though your code appears correct. Messages are not being captured and sent to the designated logging channel.

:thinking: Understanding the “Why” (The Root Cause):

The issue likely stems from a combination of outdated methods and potentially missing permissions or incorrectly configured intents. The original code uses bot.channels.get(), which is deprecated in newer versions of the discord.js library. This method might silently fail without throwing an error, preventing your bot from finding the correct channel to send log entries to. Additionally, if your bot lacks the necessary permissions (“Send Messages” and potentially “View Channel”) in the logging channel or the channels where messages are being monitored, the log entries won’t be sent. Finally, incorrect Discord Bot Gateway intents can prevent your bot from receiving message content which will result in your bot not even seeing the messages you want to catch.

:gear: Step-by-Step Guide:

  1. Update to Current Methods and Handle Errors: Replace the deprecated bot.channels.get() method with the more modern and robust bot.channels.cache.get() or bot.channels.fetch(). The cache.get() method offers speed, while fetch() ensures you receive the most current information. Wrap this in a try/catch statement to handle potential errors from fetch() or cases where the channel is not found.

    bot.on('message', async msg => {
        if (!msg.guild) return;
        if (!msg.content.includes('*report')) return;
        try {
            const logChannel = await msg.guild.channels.fetch('9876543210123456789'); // Fetch the channel
            if (!logChannel) {
                console.error("Logging channel not found!");
                return;
            }
            const logEntry = `[LOG] [${msg.guild.name}] [#${msg.channel.name}] ${msg.author.username}#${msg.author.discriminator}: ${msg.content}\n`;
            await logChannel.send(logEntry);
        } catch (error) {
            console.error("Error logging message:", error);
        }
    });
    
    
  2. Verify Bot Permissions: Ensure your bot has the “Send Messages” permission in the designated logging channel (9876543210123456789). Additionally, check if the bot needs the “View Channel” permission in the channels being monitored for messages containing ‘report’. If the bot lacks the necessary permissions, it won’t be able to send messages to the logging channel, even if the code is correct.

  3. Verify Gateway Intents: In the Discord Developer Portal, go to your bot’s application page, and under the “Privileged Gateway Intents” section, ensure the MESSAGE_CONTENT intent is enabled. This intent is required for your bot to receive the actual content of messages, which is essential for your keyword check to function correctly. Without this, your bot might receive the message event, but msg.content will be empty, causing your includes() check to always fail.

  4. Test Thoroughly: After making these changes, thoroughly test your bot with various messages, including those with and without the ‘report’ keyword, ensuring they are sent in channels where the bot has the appropriate permissions. Log messages using console.error to aid in debugging any further issues.

:mag: Common Pitfalls & What to Check Next:

  • Case Sensitivity: The includes() method is case-sensitive. Make sure the keyword ‘report’ in your code exactly matches the case used in the messages you’re testing.
  • Keyword Spacing: Ensure there are no extra spaces before or after ‘report’ in your messages.
  • Multiple Bots: Ensure you are not running multiple instances of your bot simultaneously, which can create unexpected behavior and interference.
  • Rate Limits: Discord has rate limits; excessive attempts to send messages can temporarily block your bot.

:speech_balloon: Still running into issues? Share your (sanitized) config files, the exact command you ran, and any other relevant details. The community is here to help!

Had the same problem! It’s probably the bot.channels.get() method - that’s deprecated in newer discord.js versions and fails silently. Switch to bot.channels.cache.get() or even better, use bot.channels.fetch() for more reliable results. Also check if your bot can actually send messages to that logging channel - I’ve seen code run fine but fail at sending because of missing permissions. One more thing: make sure your bot token has message content intent enabled. Discord changed those requirements recently and it’ll break things if you don’t have it.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.