Search code examples
javascriptdiscorddiscord.js

Send a direct message on private message to welcome new users who join the server


I want to get the bot to send a direct/private message to new users that join the server. It is able to post a welcome message on a channel but it keeps throwing errors for direct messages.

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

bot.on('ready', () => {
  console.log('I am ready!');
});

bot.on('guildMemberAdd', member => {
       member.guild.defaultChannel.send(`Welcome to the server, ${member}!`);
       console.log(`${member.user.username} has joined`);
});

bot.login('TOKEN_HERE');

Solution

  • Member object has a send method just like channels.

    Note sendMessage is deprecated

    const Discord = require('discord.js');
    const bot = new Discord.Client ();
    
    bot.on('guildMemberAdd', member => {
       member.send("Welcome to the server!");
    });
    
    bot.login('TOKEN_HERE');