向服务器的所有成员添加角色的代码

时间:2021-02-05 04:30:52

标签: discord discord.js

const { MessageEmbed } = require('discord.js')

module.exports = {
  name: "addrole",
  aliases: ["role", "P!role"],
  category: "moderation",
  description: "Add role to any user",
  run: async (client, message, args) => {
   if (!message.member.hasPermission("MANAGE_ROLES")) {
      return message.channel.send("sorry you need permission to mute someone");
    }
    if (!message.guild.me.hasPermission("MANAGE_ROLES")) {
      return message.channel.send("I do not have the permissions");
    } 
    const targets = message.mentions.members;
    
    if(!targets.first()) return message.reply(`<:no:677902165859237894>please mention user!`)
    
    let arole = message.mentions.roles.first();
    
    if(!arole) return message.reply(`<:no:677902165859237894>please mention role for add!`)
    
    
      const embed = new MessageEmbed()
      
      .setColor("RANDOM")
      .setDescription(`<a:ok_:731369076315652167>role added ${arole}`)
      
      await message.channel.send(embed)
      
    targets.forEach(target => target.roles.add(arole));
    
  }
}

这会为提到的用户添加角色。 相反,有没有办法改变

const targets = message.mentions.members

这样目标都是服务器成员?然后通过foreach,我可以将角色赋予所有目标。

1 个答案:

答案 0 :(得分:0)

您可以从 message.guild.members.cache 访问所有公会成员并使用 .forEach()