Reputation: 11
I'm trying to make a sipmle registration function, but I can't figure out what is the problem. This is the code:
fasz.on('messageReactionAdd', async (msg, guild, reaction, user) =>{
let registered = msg.guild.roles.find(role => role.name === "Regisztrált");
if(reaction.message.partial) await reaction.message.fetch();
if(reaction.partial) await reaction.fetch();
if(user.bot) return;
if(!reaction.message.guild) return;
if(reaction.message.channel.id === '718990681745522728'){
if(reaction.emoji.name === '👌'){
await reaction.message.guild.members.cache.get(user.id).roles.add(registered)
return user.send('Sikeres regisztráció! Üdvözlünk a szerveren!')
}
}
})
And this is the error message is get:
C:\Users\god\Documents\GitHub\discord.js\main.js:74
let registered = msg.guild.roles.find(role => role.name === "Regisztrált");
^
TypeError: Cannot read property 'roles' of undefined
at Client.<anonymous> (C:\Users\god\Documents\GitHub\discord.js\main.js:74:32)
at Client.emit (node:events:376:20)
at MessageReactionAdd.handle (C:\Users\god\Documents\GitHub\discord.js\node_modules\discord.js\src\client\actions\MessageReactionAdd.js:49:17)
at Object.module.exports [as MESSAGE_REACTION_ADD] (C:\Users\god\Documents\GitHub\discord.js\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_REACTION_ADD.js:4:37)
at WebSocketManager.handlePacket (C:\Users\god\Documents\GitHub\discord.js\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (C:\Users\god\Documents\GitHub\discord.js\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (C:\Users\god\Documents\GitHub\discord.js\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (C:\Users\god\Documents\GitHub\discord.js\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:376:20)
at Receiver.receiverOnMessage (C:\Users\god\Documents\GitHub\discord.js\node_modules\ws\lib\websocket.js:835:20)
Upvotes: 0
Views: 1856
Reputation: 1880
You can get a role
by it's name like this:
const role = reaction.message.guild.roles.cache.find(r => r.name === 'Yor role name');
After getting the role
you can get the user
and assign the role
:
const { guild } = reaction.message
const member = guild.members.cache.find(member => member.id === user.id);
member.roles.add(role);
Upvotes: -1
Reputation: 1196
Like I mentioned in the comment, the Client#messageReactionAdd
event only emits two variables. That means you cannot arbitrarily use four variables to your liking to assign it.
Code:
client.on('messageReactionAdd', async (reaction, user) => { //basically changed four to two variables
//replaced msg.guild.roles.find() with reaction.message.guild.roles.find()
let registered = reaction.message.guild.roles.find(role => role.name === "Regisztrált");
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (!reaction.message.guild) return;
if (reaction.message.channel.id === '718990681745522728') {
if (reaction.emoji.name === '👌') {
await reaction.message.guild.members.cache.get(user.id).roles.add(registered)
return user.send('Sikeres regisztráció! Üdvözlünk a szerveren!')
}
}
});
Upvotes: 2