Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

338
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda