首页 > 解决方案 > 如何为 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(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.

标签: javascriptdiscord.js

解决方案


好吧,过滤器基本上是这样工作的:我们基本上要求机器人为我们返回集合中满足特定条件的所有成员对象 - 因此我喜欢将其视为“滚动浏览所有成员并把所有 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} .`)

推荐阅读