Command Handler - Discord Welcome Embed

Viewed 84

I am trying to get my Embed to load on guildmemberAdd. This is my code below, as this is all a learning curve. I could really use come help correcting my mistake, so that when my new guild member joins, my Embed pops up automatically.

client.on('guildMemberAdd', guildMember => {
    let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'NewMember')
        
    guildMember.roles.add(welcomeRole)
    guildMember.guild.channels.cache.get('My discord channel').send(` <@${guildMember.user.id}> `)
    member.guild.channels.get('My discord channel').send('reactionrole'); 
});
1 Answers

I have noticed 2 issues within one particular line of your code:

member is not defined

You seem to look for a channel based off a member object - Which by itself is completely fine, but you have never defined the variable member itself. This can be found in the last line of your code, stating:

    member.guild.channels.get('My discord channel').send('reactionrole');

The simple fix to this is replacing member with guildMember, as it is how you defined the GuildMember object of the user who just joined your guild.

Lacking .cache

In the same line, you seem to want to find a channel based off a guild's channels list. In the not so recent discord.js update, also known as v12, you are now required to use the .cache method to receive a full Collection of an entire guild's cached channels list, unless you fetch them instead.

Final Code

client.on('guildMemberAdd', guildMember => {
    let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'NewMember')
        
    guildMember.roles.add(welcomeRole)
    /** 
    * Keep in mind neither of the below methods send an Embed. 
    * You would need to create a MessageEmbed Constructor to do so.
    */
    guildMember.guild.channels.cache.get('My discord channel').send(` <@${guildMember.user.id}> `)
    guildMember.guild.channels.cache.get('My discord channel').send('reactionrole'); 
});```
Related