如何为列为 id 的人员分配特定角色?

时间:2021-01-11 19:41:18

标签: javascript discord.js

我想选某些成员(不是全部)

我自己尝试过,但我不能。

这是我的代码:

     let role = message.guild.roles.cache.find(role => role.name === "jail");

     if (!role) return message.channel.send(`**${message.author.username}**, not found.`)

     var list = [524983207943340052, 223550304447234048]


     message.guild.members.cache.filter(list).forEach(member => member.roles.add(role))
     message.channel.send(`**${message.author.username}**,  ${role} .`)

我收到此错误:

(node:4480) UnhandledPromiseRejectionWarning: TypeError: fn is not a function
    at Map.filter (C:\Users\user\Desktop\scil bot\scil\rol\node_modules\@discordjs\collection\dist\index.js:190:17)
    at Object.run (C:\Users\user\Desktop\scil bot\scil\rol\komutlar\sicil.js:32:34)
    at module.exports.message (C:\Users\user\Desktop\scil bot\scil\rol\node_modules\proton-io\index.js:154:21)
    at Client.<anonymous> (C:\Users\user\Desktop\scil bot\scil\rol\index.js:26:16)
    at Client.emit (events.js:311:20)
    at MessageCreateAction.handle (C:\Users\user\Desktop\scil bot\scil\rol\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\user\Desktop\scil bot\scil\rol\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\user\Desktop\scil bot\scil\rol\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\user\Desktop\scil bot\scil\rol\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\user\Desktop\scil bot\scil\rol\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
(node:4480) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:4480) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

1 个答案:

答案 0 :(得分:0)

好吧,过滤器基本上是这样工作的:我们基本上是要求机器人为我们返回集合中满足特定条件的所有成员对象 - 因此我喜欢将其视为“滚动浏览”所有成员并将所有 x 成员归还给我”,拿你的代码,你基本上是在说“滚动所有成员并将所有 id 成员归还给我”,让我们说实话毫无意义。

我们想要的正确输出是要求客户端返回所有具有特定 id 的用户,因此为什么我们需要使用设置参数,我们称之为 member。从那时起,我们可以简单地使用 .includes() 函数,并从我们获取的缓存成员中确定 ID 数组是否包含某个成员的 ID。

因此,您的代码应该如下所示:

let role = message.guild.roles.cache.find(role => role.name === "jail");

 if (!role) return message.channel.send(`**${message.author.username}**, not found.`)

 var list = [524983207943340052, 223550304447234048]


 message.guild.members.cache.filter(member => list.includes(member.id)).forEach(member => member.roles.add(role))
 message.channel.send(`**${message.author.username}**,  ${role} .`)