I need to get all rôles from the user who made the reaction with a code like :
module.exports = {
name: 'messageReactionAdd',
async execute(reaction, user) {
if (user.partial) {
try {
await user.fetch();
}
catch (error) {
console.error('Erreur en récupérant le user : ', error);
return;
}
console.log(user.roles);
}
},
};
The log return "undefined"
But I have no clue how to do this correctly after reading the documentation and I don't know how many stackoverflow questions.
I hope someone can help me.
CodePudding user response:
Users don't have roles, so you can't use user.roles
to get their roles. You have to check which guild the message came from and get the member from the guild. Get the guild with reaction.message.guild
, but check if it exists first, because if the message was in a DM, then it won't have a guild.
if (reaction.message.guild) {
const member = reaction.message.guild.members.cache.get(user.id);
}
After that, you can get the member as you would normally, through the cache. Keep in mind that the member may not exist in the cache, so you also have to check if the member exists before getting their roles:
if (reaction.message.guild) {
const member = reaction.message.guild.members.cache.get(user.id);
if (member) console.log(member.roles);
}