'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);
})
Solution 1:[1]
let members = await message.guild.members.fetch()
members.filter(user => !user.bot).tap(message.guild => {
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BDL |
