isonic_07
isonic_07

Reputation: 5

How can I make a discord bot that will DM new users when they join the server?

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>');

Upvotes: 0

Views: 1690

Answers (2)

thecalendar
thecalendar

Reputation: 79

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."

Upvotes: 0

HellCatVN
HellCatVN

Reputation: 876

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>');

Upvotes: 0

Related Questions