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

329
Views
TypeError: Cannot read property 'roles' of undefined in discord.js

I am trying to get the bot to add roles in one discord and also add a role in another discord. but I keep getting the "roles" is not defined error. I have little to no coding knowledge and most of my code is a combination of things I find on google or friends teach me, so please excuse me if it is a dumb problem with a simple solution.

const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.GUILD_ROLES] });
    
    module.exports = {
        
        name: 'accept',
        description: 'Adds A Player To The Whitelist',
        permissions: `ADMINISTRATOR`,
        execute(message, args, add, roles, addRole, guild) {
                
            message.delete()
    
    
            let rMember = guild.roles.cache
            message.mentions.members.first() || // `.first()` is a function.
            message.guild.members.cache.find((m) => m.user.tag === args[0]) ||
            message.guild.members;
    
    
            let role1 =
        message.guild.roles.cache.find((r) => r.id == roleID)
    
        let role2 =
        message.guild.roles.cache.find((r) => r.id == roleID)
    
        let server = client.guilds.cache.get('guild ID')
        let memberRole = server.guild.roles.get("role ID");
    
        rMember.roles.add(role1).catch((e) => console.log(e));
        rMember.roles.add(role2).catch((e) => console.log(e));
        rMember.roles.add(memberRole).catch((e) => console.log(e));
    
    
            rMember.send(`message content`);
            message.channel.send('message content')
    }};

The error occurs in this line:

let memberRole = server.guild.roles.get("872432775330955264");
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

let server = client.guilds.cache.get('guild ID')
let memberRole = server.guild.roles.get("role ID");

These particular lines justify your error, in the first case you are assigning the property of a guild object to server by getting the guild from the cache, now you see a guild object does not have a property further named guild to it so your error actually rests in the next line, where you are trying to get from server.guild it's same as saying guild.guild which makes no actual sense.

Only correction you would want to make with your code would be something of this sort:

let server = client.guilds.cache.get('guild ID')
let memberRole = server.roles.cache.get("role ID");
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!