Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

147
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda