因此,我创建了一个机器人,该机器人将为6K +用户Discord服务器中的所有用户添加角色,因此我们仅允许具有特定角色的成员键入特定频道,即可更轻松地管理拖钓。问题是,我只想将该角色提供给服务器上已有1个以上角色的用户。
使用Discord.js,我想出了以下代码,可以完美地将角色分配给所有用户(我已经在另一台服务器上进行了测试),但我想使其专门添加给具有1个以上角色的用户已经。
感谢您的任何帮助!
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);
答案 0 :(得分:0)
如文档所述:
https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=members
一旦引用了公会,就可以使用此参数获取公会中的所有成员。然后,您可以将其过滤为仅具有多个角色的人员:
guild.members.filter(member => member.roles.array().length > 0).forEach(member => member.addRole(role));
这样的事情是一般前提,应该起作用。
答案 1 :(得分:0)
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);