Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

90
Views
DiscordJs - Member undefined when remove reaction first

im trying to make a basic bot add/remove role when add/remove reaction from a post on discord.

The code works if i add the role first and then remove it, but lets say i already have the role and restart the script. When i remove the reaction the script gives me the error saying: "Cannot read properties of undefined (reading 'roles')"

client.on('messageReactionRemove', async (reaction, user) => {
if (reaction.partial) {
    try {
        await reaction.fetch();
    } catch (error) {
        console.error('Something went wrong when fetching the message:', error);
        return;
    }
}

if (reaction.message.id != pinnedMsg) {
    return;
}
var role = reaction.message.guild.roles.cache.find(role => role.name === "ROLENAME");
const guild = reaction.message.guild;

const member = await guild.members.cache.find(member => member.id === user.id);

member.roles.remove(role); }); //var member is undefined here
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You need to fetch members with <Guild>.members.fetch because they are not cached. Don't forget to use await when needed, to find in cache it's not necessary.

Example for your case:

await guild.members.fetch();
const member = guild.members.cache.find(member => member.id === user.id);
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!