【问题标题】:Adding role to all members who has another role?向所有具有其他角色的成员添加角色?
【发布时间】:2020-09-15 19:29:22
【问题描述】:

我想在我的 discord 机器人上运行一个命令,并为所有具有预定义角色的成员添加一个辅助角色。

它会是这样的。所有具有 X 角色的成员都将被分配到 Y 角色。

我尝试编写代码但失败了,因此我将代码编写为伪代码,任何帮助都会很棒。

const userID = "1234"; //this is my userid so only I will be able to use this command
let roleID1 = "xxxx";
let roleID2 = "yyyy";
client.on("message", function (message) {
  if (message.author.id === userID) {
    if (message.content === "!setrole") {

      // Code for taking the list of all members with role xxxx.
      // Code for assigning all those people to the role yyyy.

      message.channel.send("Roles assigned");
    }
  }
});

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    试试这个:

    // iterate a function through all members of the guild
    message.guild.members.cache.forEach((member) => {
      if (member.roles.cache.has(roleID1) // if member has x role
         member.roles.add(roleID2); // give y role
    });
    

    你也可以使用Array.prototype.filter();虽然执行上没有区别,但可能会更快(但我真的不知道):

    message.guild.members
      .filter((member) => member.roles.cache.has(roleID1))
      .forEach((member) => member.roles.add(roleID2))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2019-02-15
      • 2021-11-17
      相关资源
      最近更新 更多