Hadrien Casadesus
Hadrien Casadesus

Reputation: 5

Discord JS bot crashing after sending a dm message

I want my bot to send a dm to someone executing the command !RHelp. For that I wrote this code:

if (message.content.startsWith('!RHelp')){
        client.users.fetch('<User ID>').then((user) => user.send('Hello World'));
        message.reply('I just sent you a DM');}

The bot is correctly executing the code but after sending the message he is crashing with this error:

const member = message.guild.member(user);
                             ^         

TypeError: Cannot read property 'member' of null

I alrady tried to do message.author.send('Hello World'); But it's the same problem...

I am declaring user and member in a separated file 'utiles.js':

const Discord = require('discord.js');
const Client = new Discord.Client();



module.exports ={
    getUtiles: function (message){
            const user = message.mentions.users.first();
            const member = message.guild.member(user);
            const args = message.content.trim().split(/ +/g);
            const command = args.shift().toLowerCase();
            const usrTag ='<@'+ member + '>'; //get the mentionned user tag
            
            this.user = user;
            this.member = member;
            this.args = args;
            this.usrTag = usrTag;
            
            return user, member, args, usrTag;
    }
}

Upvotes: 0

Views: 552

Answers (2)

Flappy
Flappy

Reputation: 72

Change const member = message.guild.member(user); to const member = message.member, this code worked for me, so replace the if (command === 'rhelp') { with if (message.content.startsWith('!RHelp')){ to work your your case.

    if (command === 'rhelp') {
    let member = message.member
    member.send("Hello")
     message.reply('I just sent you a DM');
  }

Upvotes: 0

Xzandro
Xzandro

Reputation: 977

Since you are doing things in a DM, there is no message.guild and thus there is no member function. You would need to check if the message.guild is null or not and then act accordingly. It's unclear how / when the getUtiles function is run so the specific solution is up to you in that regard.

Upvotes: 1

Related Questions