我想创建一个列出用户当前角色的命令。
但是当我尝试 member.roles.map(roles => `${roles}`).join(', ')
或者 message.member.roles.map(role => role.name).slice(1).join(", ")
,它会抛出这个错误:
TypeError: member.roles.map is not a function
at Object.exports.run (c:\Users\Flo\Desktop\Discord_Bot\commands\test.js:18:42)
at Client.<anonymous> (c:\Users\Flo\Desktop\Discord_Bot\index.js:32:14)
at Client.emit (c:\Users\Flo\Desktop\Discord_Bot\lib\events.js:388:22)
at MessageCreateAction.handle (c:\Users\Flo\Desktop\Discord_Bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (c:\Users\Flo\Desktop\Discord_Bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (c:\Users\Flo\Desktop\Discord_Bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
at WebSocketShard.onPacket (c:\Users\Flo\Desktop\Discord_Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
at WebSocketShard.onMessage (c:\Users\Flo\Desktop\Discord_Bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
at WebSocket.onMessage (c:\Users\Flo\Desktop\Discord_Bot\node_modules\ws\lib\event-target.js:132:16)
at WebSocket.emit (node:events:376:20) {stack: 'TypeError: member.roles.map is not a function…6)
at WebSocket.emit (node:events:376:20)', message: 'member.roles.map is not a function'}
这是造成此错误的代码:
let user;
if (msg.mentions.users.first()) {
user = msg.mentions.users.first();
} else {
user = msg.author;
}
答案 0 :(得分:2)
从 v12 开始,你需要在一些管理器之前添加 .cache,比如 guild#members.cache & client#users.cache。您所要做的就是在 .map 之前添加 .cache ,它就会起作用!
在此处阅读更多相关信息: https://discordjs.guide/additional-info/changes-in-v12.html