简体   繁体   English

如何在我的 discord 服务器上为每个提到的用户添加角色

[英]How to add roles to every mentioned users on my discord server

My current code works like this.我当前的代码是这样工作的。 If I type command @rolename @user , my bot gives the mentioned user the mentioned role.如果我输入command @rolename @user ,我的机器人会为提到的用户提供提到的角色。

But I want to add every mentioned user the mentioned role: command @rolename @user1 @user2 @user3但我想为每个提到的用户添加提到的角色: command @rolename @user1 @user2 @user3

Here is my code, but it only adds the role to the first member:这是我的代码,但它只将角色添加到第一个成员:

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 permission to mute");
    } 
    let target = message.mentions.members.first();
    
    if(!target) 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!`)
    
    let ticon = target.user.avatarURL({ dynamic: true, size: 2048 });
    let aicon = message.author.avatarURL({ dynamic: true, size: 2048 });
    
      const embed = new MessageEmbed()
      
      .setColor("RANDOM")
      .setDescription(`<a:ok_:731369076315652167>changed role for ${target.user.username} added ${arole}`)
      
      await message.channel.send(embed)
      
      target.roles.add(arole)
  }
}

message.mentions.members returns a collection so you can use the .each method to add role to every mentioned member: message.mentions.members 返回一个集合,因此您可以使用.each方法为每个提到的成员添加角色:

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 permission to mute');
}

const { members } = message.mentions;

if (!members.size) {
  return message.reply(`<:no:677902165859237894> please mention a user!`);
}

const role = message.mentions.roles.first();

if (!role) {
  return message.reply(
    `<:no:677902165859237894>please mention role for add!`,
  );
}

const embed = new MessageEmbed().setColor('RANDOM');

members.each((member) => {
  member.roles.add(role);
  embed.setDescription(
    `<a:ok_:731369076315652167> role changed for ${member.user}, added ${role}`,
  );
  message.channel.send(embed);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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