Search code examples
node.jsdiscorddiscord.jsbots

discord.js event handler welcome message


so I made an event handler for my discord bot so that the index.js file would be neat. But for some reason the welcome message that I made whenever someone joins the server doesn't work.

Here's my event handler code:

const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const event = require(`./events/${file}`);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args, Discord, client));
    } else {
        client.on(event.name, (...args) => event.execute(...args, Discord, client));
    }
}

And Here's my welcome message code:

module.exports =  {
    name: 'welcome',
    once: false,
    execute(Discord, client) {

    const welcomechannelId = '753484351882133507' //Channel You Want to Send The Welcome Message
    const targetChannelId = `846341557992292362` //Channel For Rules

        client.on('guildMemberAdd', (member) => {
            let welcomeRole = member.guild.roles.cache.find(role => role.name === 'Umay');
            member.roles.add(welcomeRole);

            const channel = member.guild.channels.cache.get(welcomechannelId)

            const WelcomeEmbed = new Discord.MessageEmbed()
            .setTitle(`Welcome To ${member.guild.name}`)
            .setThumbnail(member.user.displayAvatarURL({dynamic: true, size: 512}))
            .setDescription(`Hello <@${member.user.id}>, Welcome to **${member.guild.name}**. Thanks For Joining Our Server.
Please Read ${member.guild.channels.cache.get(targetChannelId).toString()}, and assign yourself some roles at <#846341532520153088>. You can chat in <#753484351882133507> and talk with other people.`)
         // You Can Add More Fields If You Want
            .setFooter(`Welcome ${member.user.username}#${member.user.discriminator}`,member.user.displayAvatarURL({dynamic: true, size: 512}))
            .setColor('RANDOM')
        member.guild.channels.cache.get(welcomechannelId).send(WelcomeEmbed)
        
    })

    }
}

I get no error, but whenever someone joins the server, he/she won't be given the role and the welcome message doesn't appear. I put the welcome message code on an events folder that the event handler is handling. Can anyone help?


Solution

  • The issue lies within the welcome code.

    In the handler code you have the following line:

    client.on(event.name, (...args) => event.execute(...args, Discord, client));
    

    This triggers the client on the name property set in the welcome code.

    You currently have it set to welcome, which is not a valid event. The bot is now listening for a welcome event, which will never happen.

    First course of action is setting the name property to guildMemberAdd like this:

    module.exports =  {
        name: 'guildMemberAdd',
    //the rest of the code
    

    Then you have another issue. Within the welcome code you have client.on() again.

    This will never work, unless by some incredibly lucky chance 2 people join within a millisecond of each other, but even then you'll only have 1 welcome message.

    Removing the following:

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

    will fix that issue.

    Then the last thing to do is having the member value being imported correctly. We do this by changing the following line:

    execute(Discord, client) {
    

    to:

    execute(member, Discord, client) {
    //code
    

    The resulting code will look like this:

    module.exports =  {
        name: 'guildMemberAdd',
        once: false,
        execute(member, Discord, client) {
    
        const welcomechannelId = '753484351882133507' //Channel You Want to Send The Welcome Message
        const targetChannelId = `846341557992292362` //Channel For Rules
    
                let welcomeRole = member.guild.roles.cache.find(role => role.name === 'Umay');
                member.roles.add(welcomeRole);
    
                const channel = member.guild.channels.cache.get(welcomechannelId)
    
                const WelcomeEmbed = new Discord.MessageEmbed()
                .setTitle(`Welcome To ${member.guild.name}`)
                .setThumbnail(member.user.displayAvatarURL({dynamic: true, size: 512}))
                .setDescription(`Hello <@${member.user.id}>, Welcome to **${member.guild.name}**. Thanks For Joining Our Server.
    Please Read ${member.guild.channels.cache.get(targetChannelId).toString()}, and assign yourself some roles at <#846341532520153088>. You can chat in <#753484351882133507> and talk with other people.`)
             // You Can Add More Fields If You Want
                .setFooter(`Welcome ${member.user.username}#${member.user.discriminator}`,member.user.displayAvatarURL({dynamic: true, size: 512}))
                .setColor('RANDOM')
            member.guild.channels.cache.get(welcomechannelId).send(WelcomeEmbed)
    
        }
    }
    

    Happy coding!