Home > database >  Why doesn't my welcome message work(discord.js)
Why doesn't my welcome message work(discord.js)

Time:10-20

Im trying to make a bot that gives a user member role and sends a simple welcome message to the chat. However when someone joins nothing happens(not even an error). Here is my code:

client.on('guildMemberAdd', guildMember => {
        console.log(guildMember);
        let role = guildMember.guild.roles.cache.find(role => role.name == 'member');
        guildMember.roles.add(role);
    
        member.guild.channels.get('channelID').send(`welcome <@${guildMember.id}> dont forget to 
        check the rules`);
    });

CodePudding user response:

Did you activate the necessary flags ? A client needs the following intent in order to get alerted when someone joins a server the bot is in

const client = new Discord.Client({intents : ["GUILD_MEMBERS"]})

See more intents in the official documentation

In the end, it would look something like :

const Discord = require('discord.js')
const client = new Discord.Client({intents : ["GUILD_MEMBERS"]})

client.on('guildMemberAdd', guildMember => {
        console.log(guildMember);
        let role = guildMember.guild.roles.cache.find(role => role.name == 'member');
        guildMember.roles.add(role.id);
    
        guildMember.guild.channels.get('the channel id')).send(`welcome <@${guildMember.id}> dont forget to 
        check the rules`);
    });

Also make sure you didn't put the client.on('guildMemberAdd',...) inside the client.on('message',..) They need to be separated.

  • Related