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);
})
let members = await message.guild.members.fetch()
members.filter(user => !user.bot).tap(message.guild => {
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.