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

153
Views
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 answers
Answer question

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 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!