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

502
Views
Mass role assign to users (discord.js v13)

This code assigns role to a list of users separated by new line, but only users in cache. How can I implement message.guild.members.fetch() to this code to assign role to all users?

//command
!roleAssign @role
name#1234
name#2234
name#3234
const roleAssign = function(message){
  const Role = message.mentions.roles.first();
  // split by new line
  const users = message.content.slice(prefix.length).split(/\r?\n/).slice(1);
  const success = [];

  users.forEach(user => {
    const u = client.users.cache.find(u => u.tag === user);
    if(u){
      const userId = u.id;
      // person with the userID
      let person = message.guild.members.cache.get(userId);
      // give role to person

      try{
        person.roles.add(Role)
      }
      catch(e){console.error(e)};

      if(person.roles.cache.has(Role.id)){
        success.push(user);
      }
      }
  });

  message.channel.send(`Added role ${Role.name} to\n${success.map(user => user).join("\n")}`);
}

I tried this but got TypeError: Cannot read properties of undefined (reading 'find')

message.guild.members.fetch().then((members)=>{
  message.cache.find(u => u.tag === user);
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

members.filter(user => !user.bot).tap(message.guild => {

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