Node.js Discord bot not deleting specific messages correctly

I’m working on a Discord bot using Node.js, and I need it to detect messages containing a specific string, *DD, and avoid deleting them. Right now, I’m trying to implement a feature that allows for automatic deletion of messages after a set time, but also skip those containing this specific string.

Here’s the code I’m working with:

const Discord = require('discord.js');
const bot = new Discord.Client();

bot.on('message', function(message) {
  const content = message.content.toUpperCase();
  if(content.includes('*DD')) {
    // Skip deletion
  } else {
    setTimeout(function() {
      message.delete();
    }, 120000);
  }
});

The intended functionality is to keep messages with *DD intact while deleting others after two minutes. However, I keep encountering a ReferenceError for an undefined variable. Any guidance on how to rectify this issue would be greatly appreciated.

you’re missing error handling. add .catch() to your delete call or wrap it in try/catch. also check if the message exists before deleting - users sometimes delete their own messages first, which causes that reference error.

You’re holding onto the message object for 2 minutes, which breaks when Discord’s internal references change. You’re also not handling cases where the message might already be deleted.

I hit this exact issue in production and fixed it by moving the logic to an external automation platform instead of cramming everything into the bot.

Have your bot send webhook notifications when messages arrive. Then use Latenode for the deletion logic with proper error handling and retries. Create a workflow that waits 2 minutes, checks if the message exists, verifies it doesn’t contain *DD, then deletes it via API.

Way more reliable since Latenode handles timing issues and API failures. Your bot stays simple and you get better monitoring of what’s happening with deletions.

Webhook integration takes 5 minutes to set up and you can test everything without redeploying the bot.

That ReferenceError is probably from using an old Discord.js version. Your code looks like v11 or v12, but the syntax doesn’t match up right. I ran into the same thing when switching versions. If you’re on v13+, you’ll need to update how you initialize the client and use message.deletable to check before deleting. Definitely wrap your delete in a try-catch - other users or bots might’ve already deleted the message by the time your timeout runs. Also, your message reference can go stale after 2 minutes. I’ve had better luck storing the message ID and channel, then fetching and deleting with those instead of keeping the original message object around.

Your problem is storing message references in memory too long. When Discord’s gateway resets or your bot restarts, those message objects break and throw ReferenceErrors. I’ve hit this same issue before. Don’t use setTimeout with message objects directly - it’s unreliable. Instead, save the message ID, channel ID, and timestamp to a database or JSON file. Then run a check every 30 seconds to handle deletions. This survives restarts and plays nicer with Discord’s rate limits. Also, your current setup won’t tell you when deletions fail because the bot lacks permissions or mods already removed the message.

ReferenceError happens because Discord garbage collects message objects or when your bot disconnects. Had this exact problem building a mod bot last year. Your setTimeout keeps dead references that blow up later. Don’t store the whole message object - grab what you need right away and use scheduled tasks instead. Save message.id, message.channel.id, and Date.now() in a Map, then setInterval every 10-15 seconds to check timestamps and delete old messages. Way more reliable and won’t break when network hiccups. Also check if the message still exists with channel.messages.fetch() before deleting - users might’ve already deleted it themselves.