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

149
Vistas
Mass assign roles to the list of users discord.js v13

This code assigned specified role to a list of members. The problem when I try to get the member with userID from message.guild.members.

Code

// !roleAssign @role 
const roleAssign = async function(message){
  const Role = message.mentions.roles.first();
  // split by new line
  const users = message.content.slice(prefix.length).split(/\r?\n/).slice(1);

  let members = await message.guild.members.fetch();

  users.forEach(user => {
    const u = client.users.cache.find(u => u.tag === user);
    if(u){
      const userId = u.id;
  
      // member with the userID
      let member = members.cache.get(userId);

      // give role to the member
      try{
        member.roles.add(Role)
      }
      catch(e){console.error(e)};
  });

} 

I get this error. How do I use get function with fetched members?

let member = members.cache.get(userId);
                            ^
TypeError: Cannot read properties of undefined (reading 'get')
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's because guild.members.fetch() returns a collection, not a GuildMemberManager that has a cache property. It means, you can use Collection#get() like this, without the cache:

let member = members.get(userId);
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