How can I detect private messages and send automated replies using discord.js?

I’m working on a Discord bot and need help with handling direct messages. I want my bot to recognize when someone sends it a private message and then reply back to that person.

I’ve been experimenting with different approaches but I’m having trouble with the logic for detecting DMs properly. Here’s what I’ve tried so far:

// Command handler for user support requests
client.on('message', (msg) => {
if(msg.channel.type === 'dm') {
     // Look for the keyword "support"
     if(msg.content.toLowerCase() === 'support') {
        console.log('Player ' + user.tag + ' needs help. Notifying moderators!');
        client.channels.cache.get("123456789012345678").send('**' + user.tag + '** needs moderator assistance!');
        msg.reply('Got it! A moderator will contact you shortly!');
     } else {
         msg.reply('For assistance, please send "support" in this DM!');
     }
    }
});

Some parts of my code work fine like message sending and logging, but I can’t get the DM detection working correctly. Any suggestions would be helpful.

Yeah, the user.tag reference is definitely broken like others mentioned, but there’s more. The ‘message’ event is deprecated - use ‘messageCreate’ instead.

I hit the same issues building my first support bot last year. Fix that variable, but also add error handling for the channel cache lookup. If that channel ID goes bad or the bot loses access, your whole handler crashes.

Also check !msg.author.bot along with the DM type check. Otherwise you’ll respond to other bots - learned that one the hard way when mine started chatting with automated systems.

For production, definitely add a cooldown system. Users spam the support keyword and you’ll flood your mod channel with duplicate notifications without rate limiting.

Yeah, you’re right about the user variable - should be msg.author.tag. But there’s another issue you might hit. Your channel type check could break with newer discord.js versions. I ran into this when updating from v12 to v13. The channel type comparison changed from strings to enums, so msg.channel.type === 'dm' becomes msg.channel.type === 'DM' or better yet, msg.channel.type === ChannelType.DM if you’re on v14. Also, ditch the deprecated message event for messageCreate. Same functionality but future-proofs your bot. I kept my old handler running until Discord dropped support and had to scramble updating everything. One more thing - test that channel cache lookup. That channel ID needs to exist and your bot needs send permissions, otherwise the whole thing fails silently without proper error handling.

Your issue is using user.tag instead of msg.author.tag. That’s why the DM detection isn’t working.

Here’s the fix:

client.on('message', (msg) => {
    if(msg.channel.type === 'dm') {
        if(msg.content.toLowerCase() === 'support') {
            console.log('Player ' + msg.author.tag + ' needs help. Notifying moderators!');
            client.channels.cache.get("123456789012345678").send('**' + msg.author.tag + '** needs moderator assistance!');
            msg.reply('Got it! A moderator will contact you shortly!');
        } else {
            msg.reply('For assistance, please send "support" in this DM!');
        }
    }
});

That said, managing bot interactions manually gets messy quick. I’ve built similar Discord bots for customer support and the code becomes a nightmare when you add multiple keywords, different response flows, and third-party integrations.

Now I just use Latenode to automate the whole Discord bot workflow. You can set up visual automation that detects DMs, processes content, sends responses, and connects to your ticketing system or database without the complex code.

It handles all the message routing visually, so no more debugging event handlers and channel types. Plus, it connects with tons of other services if you need to log tickets or send notifications elsewhere.

Same issue here last week lol. The msg.author.tag fix works, but also enable MESSAGE_CONTENT_INTENT or your bot won’t read DM content. Discord changed this recently - I debugged for hours before realizing my bot couldn’t see message text. Also check if the user disabled DMs, otherwise your reply throws an error.