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

127
Views
TypeErr: Cannot read property 'roles' of undefined

I am trying to create a button system where if you do not have a specific role, you are unable to use a button. However, the code I have created doesn't seem to work.

collector.on('collect', async interaction => {
        const wikiStaffRole = interaction.user.guild.roles.cache.find(role => role.name == "Staff");

        if (interaction.customId === "yes") {
          if (!wikiStaffRole) {
            return interaction.followUp({ content: `${interaction.user} dude you need to be a staff member`, ephmeral: true });
          }
          newUserEmbed.edit({ embeds: [], content: 'This report has been marked as completed.', components: [] });
          return;
         } else if (interaction.customId === "no") {
          if (!wikiStaffRole) {
            return interaction.followUp({ content: `${interaction.user} dude you need to be a staff member`, ephmeral: true });
          }
          newUserEmbed.edit({ embeds: [], content: 'This report has been denied.', components: [] });
          return;
        } else if (interaction.customId === "what") {
          if (!wikiStaffRole) {
            return interaction.followUp({ content: `${interaction.user} dude you need to be a staff member`, ephmeral: true });
          }
          newUserEmbed.edit({ embeds: [], content: 'This report has been marked as inconclusive.', components: [] });
          return;
        }
        });

Instead, I just get an error that states that it cannot read the property 'roles' of undefined. I cannot figure out any ways to fix this.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You tried accessing the non-existent guild property of a User object. This property does not exist on Users but does exist on GuildMember and Interaction objects. This code should work:

const wikiStaffRole = interaction.guild.roles.cache.find(role => role.name == "Staff")

Keep in mind that if it is not in a guild, this will throw an error. You can use optional chaining (?.) to prevent that

const wikiStaffRole = interaction.guild?.roles.cache.find(role => role.name == "Staff")

I think you wanted to check if the member had the role though. You are actually checking if the entire server has the role. This will check only the member's roles

const wikiStaffRole = interaction.member?.roles.cache.find(role => role.name == "Staff")
//optional chaining because member would be null if it was sent in a DM
about 4 years ago · Juan Pablo Isaza 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!