When I try to send a dm with my discord bot through discord.js, it successfully sends the message in dms and replies to my message that it had sent the dm, but then crashes in terminal, I have tried multiple things to fix it, but all doesn’t seem to work. I have tried to search on google to see if others experience the same, but no one else does, it looks like I’m the only one.
I tried a lot of different methods to fix it, doesn’t seem to work.
My code:
module.exports = {
name: "dm",
description: "DMs a user a message",
async execute(message, args, Discord){
const user = message.mentions.members.first();
const msg = args.slice(1).join(" ");
if (!user) return message.channel.send('you must ping whoever you want to send the dm to');
if (!msg) return message.channel.send("no message was written to send");
try {
if(message.member.roles.cache.has('1267019315337760810')){
if(user){
const memberTargter = message.guild.members.cache.get(user.id);
memberTargter.send(msg);
message.channel.send('sent');
}
} else{
message.channel.send('you dont have permission to do that')
}
} catch (error){
console.log(error)
}
}
}
What was supposed to happen is that it would send the DM flawlessly, but what actually happened is this crash error:
Full terminal error:
node:events:497
throw er; // Unhandled 'error' event
^
TypeError: Cannot read properties of null (reading 'id')
at Client.<anonymous> (C:UsersEL HAMDDesktopfire-department-botindex.js:51:55)
at Client.emit (node:events:531:35)
at MessageCreateAction.handle (C:UsersEL HAMDDesktopfire-department-botnode_modulesdiscord.jssrcclientactionsMessageCreate.js:28:14)
at module.exports [as MESSAGE_CREATE] (C:UsersEL HAMDDesktopfire-department-botnode_modulesdiscord.jssrcclientwebsockethandlersMESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (C:UsersEL HAMDDesktopfire-department-botnode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:355:31)
at WebSocketManager.<anonymous> (C:UsersEL HAMDDesktopfire-department-botnode_modulesdiscord.jssrcclientwebsocketWebSocketManager.js:239:12)
at WebSocketManager.emit (C:UsersEL HAMDDesktopfire-department-botnode_modules@vladfranguasync_event_emitterdistindex.cjs:287:31)
at WebSocketShard.<anonymous> (C:UsersEL HAMDDesktopfire-department-botnode_modules@discordjswsdistindex.js:1190:51)
at WebSocketShard.emit (C:UsersEL HAMDDesktopfire-department-botnode_modules@vladfranguasync_event_emitterdistindex.cjs:287:31)
at WebSocketShard.onMessage (C:UsersEL HAMDDesktopfire-department-botnode_modules@discordjswsdistindex.js:1005:14)
Emitted 'error' event on Client instance at:
at emitUnhandledRejectionOrErr (node:events:402:10)
at process.processTicksAndRejections (node:internal/process/task_queues:84:21)
Node.js v20.16.0
[nodemon] app crashed - waiting for file changes before starting...
Tuf Kourio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.