Discord bot crashes immediately after sending a dm to anyone

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...

New contributor

Tuf Kourio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật