0

So I'm new to writing code and I've hit a wall. I've looked everywhere but can't figure out how to get these commands to work together. I use Visual Studio Code or discord.js whatever its called. And I'm completely new to coding.

This code is the only one I could figure out to send me a dm

bot.on("guildMemberRemove", member => {
    bot.users.get("485655477812723712").send(member + 'has left the 
server')
}) 



bot.on("guildMemberRemove", member => {
    bot.users.get("485655477812723712").send(member + 'has left the 
   server')
    }) //.. this will send me a dm informing me that someone left

});

bot.on('message', message=>{

    let args = message.content.substring(PREFIX.length).split(" ");

    let member = message.mentions.members.first();
    switch(args[0]){
//.. these go onto my case '' commands

SO i am able to start my bot. As a test run I have another account join without problem and then I kick that account out. But it shuts the bot down saying

"TypeError: Cannot read property 'first' of null"

The same will happen if the account leaves. However, I still get notified that that person left. And I'm lost about what I should do. Help please

Manish Balodia
  • 1,863
  • 2
  • 23
  • 37

1 Answers1

2

From what i can see, the error is giving you exactly your issue. It's trying to get first on a null object. To mitigate this, try checking that the object is not null first. You can do this with:

//If it is not null
if(message.mentions.members){
    let member = message.mentions.members.first();

    //Insert the rest of the code here
}
Snel23
  • 1,381
  • 1
  • 9
  • 19