简体   繁体   中英

Only assign role to users with 1+ roles in discord.js?

So I created a bot that will add a role to all users in a 6K+ user Discord server, so we can manage trolling easier with only allowing members with a certain role to type in specific channels. Problem is, I want to only give the role to users with 1+ roles already on the server.

Using Discord.js, I have come up with the following code that works perfect to give the role to all users (I have tested on another server), but I want to make it specifically only add to users with 1+ roles already.

Thanks for any help in advance!

 const Discord = require("discord.js"); const client = new Discord.Client(); const config = require("./config.json"); client.on("ready", () => { console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`); }); client.on("message", async message => { if(message.author.bot) return; if(message.content.indexOf(config.prefix) !== 0) return; const args = message.content.slice(config.prefix.length).trim().split(/ +/g); const command = args.shift().toLowerCase(); if(command === "addalltorole") { process.setMaxListeners(0); let role = message.guild.roles.find(r => r.name == 'Community') if (!role) return message.channel.send(`**${message.author.username}**, role not found`) message.guild.members.filter(m => !m.user.bot).forEach(member => member.addRole(role)) message.channel.send(`**${message.author.username}**, role **${role.name}** was added to all members`) } }); client.login(config.token); 

As the documentation says:

https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=members

You can use this parameter to get all the members in a guild once you have a reference to a Guild. You can then filter it down to just those with more than one role:

guild.members.filter(member => member.roles.array().length > 0).forEach(member => member.addRole(role));

Something like this is the general premise and should work.

 const Discord = require("discord.js"); const client = new Discord.Client(); const config = require("./config.json"); client.on("ready", () => { console.log(`Bot has started, with ${client.users.size} users, in ${client.channels.size} channels of ${client.guilds.size} guilds.`); }); client.on("message", async message => { if(message.author.bot) return; if(message.content.indexOf(config.prefix) !== 0) return; const args = message.content.slice(config.prefix.length).trim().split(/ +/g); const command = args.shift().toLowerCase(); if(command === "addalltorole") { process.setMaxListeners(0); let role = message.guild.roles.find(r => r.name == 'BIGROLE') if (!role) return message.channel.send(`**${message.author.username}**, role not found`) message.guild.members.filter(member => member.roles.array().length > 1).forEach(member => member.addRole(role)); /* message.guild.members.filter(m => !m.user.bot).forEach(member => member.addRole(role)) */ message.channel.send(`**${message.author.username}**, role **${role.name}** was added to all members`) } }); client.login(config.token); 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM