This will be my third question of the day because I am obviously not smart with this. I am using discord.js v12 and my issue this time is with an event handler. The event handler has worked fine so far with things so far like the message or ready. I am right now trying to get a guildMemberAdd event that gives a new person that joins a role and then sends a message in the logs chat indicating they joined. When a user joins, the bot crashes. The guildMemberAdd works on my main index.js file, but won't work under the event handler.
crash log: https://imgur.com/a/MrMi536
here are a few pieces of code if they are needed.
Event Handler:
const { readdirSync } = require('fs');
module.exports = (client) => {
const load = dirs => {
const events = readdirSync(`./events/${dirs}/`).filter(d => d.endsWith('.js'));
for(let file of events){
const event = require(`../events/${dirs}/${file}`);
let eventName = file.split('.')[0];
client.on(eventName, event.bind(null, client));
}
}
['client', 'guild'].forEach(x => load(x));
}
guildMemberAdd:
module.exports = (Discord, client, guildMember) => {
console.log(guildMember);
guildMember.roles.add(guildMember.guild.roles.cache.find(role => role.name === "Member"));
guildMember.channels.cache.get('767090422626779149').send('test');
}
index.js
// heads up that I'm using discord.js v12.4.1 because v13.1.0 is weird and I don't understand it lol
const fs = require('fs');
const Discord = require('discord.js');
require('dotenv').config();
const client = new Discord.Client(); // client = name of bot
// client.on('guildMemberAdd', (guildMember) => {
// guildMember.roles.add(guildMember.guild.roles.cache.find(role => role.name === "Member"));
// });
['aliases', 'commands'].forEach(x => client[x] = new Discord.Collection());
['command', 'event'].forEach(x => require(`./handlers/${x}`)(client));
client.login(process.env.CLIENT_TOKEN); // SECRET TOKEN
I haven't used discord.js or JavaScript in at least a year so any help is appreciated.
There seems to be a problem passing the guildMember object.
Maybe changing this can help you:
client.on(eventName, event.bind(null, client)); //change this
client.on(eventName, (...args) => event.bind(null, client, ...args)) //to this
Note: I am also not 100% sure if you can use
guildMemberor if you have to usemember.