Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

javascript - Access guildMember via event handler

I want to ask how to access a guildMember via an event handler.

Here is how I'm supposed to write it (and so many others have done this too)...

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

    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get('TheChannelID').send(`Welcome, <@${guildMember.user.id}>, to our server! ... Check out the rules-cmd channel!`);
});

...but I have an event handler, shown below...

//My event handler
const fs = require('fs');

module.exports = (client, Discord) =>{
    const load_dir = (dirs) =>{
        const event_files = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith('.js'));
        
        for (const file of event_files) {
            const event = require(`../events/${dirs}/${file}`);
            const event_name = file.split('.')[0];
            client.on(event_name, event.bind(null, Discord, client));
        }
    }

    ['client', 'guild'].forEach(e => load_dir(e));
}

and here is my ATTEMPT at trying to recreate this in a separate guildMemberAdd.js file, but nothing seems to be working...

module.exports = (Discord, client, message) =>{
    const guildMember = Discord.GuildMember;
    let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'Member');

    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get('TheChannelID').send(`Welcome, <@${guildMember.user.id}>, to our server! ... Check out the rules-cmd channel!`);
}

I have scrunched up my head for hours pondering the problem, can anybody help please?

Oh, and I got most of my code from CodeLyon. He taught it to us this way...

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

    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get('TheChannelID').send(`Welcome, <@${guildMember.user.id}>, to our server! ... Check out the rules-cmd channel!`);
});

...but he didn't tell us how to do it when he made his event handler. I fixed the member counter problem that people were asking in the comments, but I can't seem to fix this problem.

Here's CodeLyon's tutorials... https://www.youtube.com/playlist?list=PLbbLC0BLaGjpyzN1rg-gK4dUqbn8eJQq4

PLEASE it will be of great appreciation if someone could help!

question from:https://stackoverflow.com/questions/65950673/access-guildmember-via-event-handler

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your attempt is almost working, your setup has it so that the arguments are in this order: (Discord, client, [parameters of the event here])

This means that you need to change your

module.exports = (Discord, client, message) =>{

to

module.exports = (Discord, client, guildMember) =>{

as your "message" for guildMemberAdd event currently is the guildmember object, so to avoid confusion, changing it will make it work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...