“ DeprecationWarning:Collection#find:而是传递一个函数”

时间:2019-11-14 01:05:02

标签: javascript discord discord.js

我正在尝试使bot dm具有特定角色的所有成员,但出现错误:DeprecationWarning: Collection#find: pass a function instead

下面是代码:

  //dm
  if (command === "dm") {
    const args = message.content.split(" ");
    const roleArgs = args.slice(0, 1);
    const messageArgs = args.slice(1)
    
    const role = message.guild.roles.find("Members")
    if (!role) return message.reply('There is not such a role!');
    
    for (let i = 0; i < message.guild.members.size; i++) {
        if (message.guild.members.roles.has(role.id)) {
            message.guild.members.user.send(messageArgs.join(" "))
        }
    }
  }

1 个答案:

答案 0 :(得分:2)

您必须使用查找功能。替换此行:

const role = message.guild.roles.find("Members")

此行:

const role = message.guild.roles.find(r => r.name === "Members");

编辑: 对于其他问题,您正在尝试访问多个成员的角色。那行不通。将此替换为for循环:

message.guild.members.forEach(m => if(m.roles.has(role.id)) { m.user.send(messageArgs.join(" ")) });