I need to make a command that first removes the member from all roles that start with an asterisk and then assign a role that would be passed in the argument of the command.
exports.run = (client, message, [where]) => {
var role = message.guild.roles.cache.find(role => role.name === where);
if (role && where.substring(0, 1) == '*') {
//remove all roles that start with * from the member, probably some kind of for loop?
message.member.roles.add(role);
message.channel.send(message.member.nickname + ' entered the channel ' + where + '.');
}
else message.channel.send('Channel' + where + ' does not exist or does not start with *.');
};
Can you help me with that for loop? Thanks.
Your solution is:
const roles = message.member.roles.cache.array();
for (var i = 0; i < roles.length; i++) {
if (roles[i].name.substring(0, 1) == "*") {
message.member.roles.remove(roles[i]);
}
}
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.