Reputation: 5
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
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:
Upvotes: 0
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