Discord.js bot terminates unexpectedly after private message delivery

I’m having trouble with my Discord bot built using discord.js. The bot works fine when sending private messages to users, but right after it delivers the message and confirms delivery, it crashes completely. The error happens every time I use the private messaging feature.

I’ve been trying to debug this for hours but can’t figure out what’s causing it. The bot sends the message successfully and even replies that it worked, but then immediately throws an error about reading properties of null.

Here’s my command code:

module.exports = {
    name: "message",
    description: "Sends a private message to a user",
    async execute(msg, arguments, Discord){

        const targetUser = msg.mentions.members.first();
        const content = arguments.slice(1).join(" ");

        if (!targetUser) return msg.channel.send('Please mention the user you want to message');
        if (!content) return msg.channel.send("Please provide a message to send");

        try {
            if(msg.member.roles.cache.has('1267019315337760810')){
                if(targetUser){
                    const recipient = msg.guild.members.cache.get(targetUser.id);
                    recipient.send(content);
                    msg.channel.send('Message delivered successfully');
                }
            } else{
                msg.channel.send('You lack the required permissions for this action')
            }
        } catch (err){
            console.log(err)
        }
    }
}

The error I’m getting points to line 51 in my main index.js file and mentions something about null properties. Has anyone experienced similar issues with Discord bots crashing after DM operations?

Your code’s missing await on the recipient.send(content) call, which could cause timing issues. But the real culprit is probably your bot processing its own confirmation message. When you send ‘Message delivered successfully’, your main message handler tries to process that too if it’s not filtering out bot messages. Since it’s looking for mentions and arguments that don’t exist in the confirmation, it crashes trying to access null properties. Just add if (msg.author.bot) return; at the start of your message handler so the bot ignores itself. Same thing happened to me - my bot kept trying to process its own responses and crashed every single time.

The crash is probably in your main index.js file, not the command code you showed. When you send a DM, Discord creates a new DM channel object. If your bot has event handlers listening for messages, they might try to access properties that don’t exist in DMs. I had the same issue - my message handler was trying to access msg.guild or msg.member after DM operations, but these are null in private messages. Check your event listeners in index.js around line 51. You probably need null checks for guild-specific properties. Also, await the recipient.send(content) call and handle failures if the user has DMs disabled.

ur already getting targetUser from mentions.members.first(), so no need to call msg.guild.members.cache.get(targetUser.id) again. just use targetUser.send(content) directly. also, wrap the DM send in a try-catch block - users can disable DMs n it’ll throw an error if u don’t handle it.