Home > Back-end >  ReferenceError: member is not defined in Discord bot
ReferenceError: member is not defined in Discord bot

Time:07-09

const { MessageEmbed} = require('discord.js')

module.exports = {
  
      name: 'guildMemberAdd',
      execute(message, Discord, client, guild) {
      const channelId = '918746332435468288' // welcome channel
      const embed = new MessageEmbed()
      .setTitle(`Welcome ${member.user.username}`)
        .setColor(`EEA83B`)
        .setDescription(`Welcome \`${member.username}\` To The Floppacord! Have Fun And Be Sure To Follow The Rules In <#860428690034917406>`)
        .setThumbnail(guild.avatarURL)
        .setFooter({ text: member.name   "#"   member.discriminator, iconURL: member.avatarURL})
  channel.send(member.tag)
  channel.send(embed)
      }}

Hey, sorry for the bad coding, im new to this sort of thing. But when I try using this in my bot, i get this error: [Bot] ReferenceError: member is not defined. Any help would be appreciated :)

P.s Im using discord.js v12.5.3 if thats important

CodePudding user response:

When creating an event listener for the guildMemberAdd event, you would get a parameter which is the member who joined the server by default. So if you had an event listener like this, you could just use the member without defining it anywhere:

client.on('guildMemberAdd', (member) => {
    // ...
})

Looking at your code, it seems like you have a command handler and have tried to put an event in it, which I don't think it would work. Another way you could do this is if you created the event listener in your main file (eg: index.js) and then execute a piece of code when the event runs. An example:

index.js:

import guildMemberFunctionFile from 'your/file/path/tothecommand'
\\ ...

client.on('guildMemberAdd', (member) => {
    guildMemberFunctionFile.execute(member)
})

\\ ...

Your event code:

function execute(member) {
    // Execute something
}
exports.execute = execute
  • Related