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?