'Private chat user on server join on discord

how can code a discord bot to private chat a new user that just join my server. this is my code but not working

client.on('guildMemberAdd', member =>{
    member.send("welcome to this server");
})


Solution 1:[1]

Your code looks fine, but you need to check to see if you have the right guild intents.

// Make sure you have Intents.FLAGS.GUILDS and Intents.FLAGS.GUILD_MEMBERS
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] });

Then you should be able to add the listener.

client.on('guildMemberAdd', member =>{
    member.send("Welcome to Sparta!");
})

Privileged Gateway Intents Also, this is a privileged intent, so you have to make sure you are allowing it in the Discord Developer Portal. To do that, navigate to Developer Portal>Select your bot> Select "Bot" (instead of General Information) > Enable Privileged Gateway Intents: Members.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JeremiahDuane