0

So basically I've been working on this one bot for my server, I want it to DM the users that join the server, Like whenever a user joins my server, they would receive a DM by my bot? I have used this code now, but it doesn't seem to work, can anyone help?

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

client.once('ready', () => {
    console.log('Bot is ready!');


bot.on("guildMemberAdd", member => {
    member.send("Welcome to the server!")
        .catch(console.error);

    });});

client.login('<token>');
isonic_07
  • 5
  • 2

2 Answers2

0

you are using wrong way it is client not bot. Cause you are initial your bot as client since const client = new Discord.Client();. And there is no need to wrap it in ready event


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

client.once('ready', () => {
    console.log('Bot is ready!');
});

client.on("guildMemberAdd", member => {
    console.log("new member join")
    member.send("Welcome to the server!").catch(console.error);
});


client.login('<token>');

HellCatVN
  • 866
  • 1
  • 6
  • 11
  • Oh, I see but when I added the code that you edited and have pasted here, It gave me an error and it still doesn't seem to work, The bot came online but when any user joins it doesn't dm anything. – isonic_07 Jun 10 '21 at 10:07
  • Try my new edit code check if it log data – HellCatVN Jun 10 '21 at 10:28
  • Make sure that when you invite the bot, it has the "server members intent" box checked – MrMythical Jun 10 '21 at 20:45
0

With the updated changes (in v14), the enums have changed to Pascal case, and the intents need to be specified like the following:

Intents: Intents.FLAGS.GUILD_MESSAGES -> GatewayIntentBits.GuildMessages

This is my code which sends a dm to the user:

const { Client, GatewayIntentBits, Partials } = require('discord.js');

const client = new Client(
    { intents: [GatewayIntentBits.GuildMembers], partials: [Partials.Channel] });
const token = 'token';

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('guildMemberAdd', (member) => {
  member.send('Welcome to the server! Feel free to introduce yourself and enjoy your stay.');
});

client.login(token);

Additionally, you need to enable the "Server Members Intent" and "Presence Intent" for your bot in the Discord Developer Portal:

  1. Go to the Discord Developer Portal: https://discord.com/developers/applications Select your application.
  2. Navigate to the "Bot" tab. Scroll down to the "Privileged Gateway Intents" section.
  3. Enable both "Server Members Intent" and "Presence Intent."