【问题标题】:Recieving a TypeError (Not a Role/Snowflake) in Discord.js在 Discord.js 中接收 TypeError(不是角色/雪花)
【发布时间】:2020-04-08 00:13:18
【问题描述】:

我看到/尝试过的一些东西:

我正在尝试在我的机器人中创建一个装饰角色部分。我当前的代码是:

var role = message.guild.roles.find( r => r.name === args[0].join(" "));
member.addRole(role).catch(console.log);

除了没有 .join(" ") 之外,我之前的代码是相同的。我收到以下错误:

TypeError: Supplied parameter was neither a Role nor a Snowflake.
    at GuildMember.addRole (/home/discord/node_modules/discord.js/src/structures/GuildMember.js:454:38)
    at Client.client.on.message (/home/discord/Desktop/channel.js:76:10)
    at Client.emit (events.js:194:15)
    at MessageCreateHandler.handle (/home/discord/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (/home/discord/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (/home/discord/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (/home/discord/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
    at WebSocket.onMessage (/home/discord/node_modules/ws/lib/event-target.js:120:16)
    at WebSocket.emit (events.js:189:13)
    at Receiver.receiverOnMessage (/home/discord/node_modules/ws/lib/websocket.js:789:20)
Created new role with name test and color 16777215

“创建的新角色”来自创建它的行。请注意,此消息是在我的角色创建脚本中使用 .then 发送的。我怎样才能让它工作?请注意,它可以很好地创建我的角色,只是无法将其添加到执行命令的用户中。使用 await 不起作用,因为 addRole 不是异步函数。

更新:我想出了如何做到这一点:使用第二个 .then 来传递 role.id。感谢您的建议!

【问题讨论】:

    标签: javascript node.js discord discord.js roles


    【解决方案1】:

    是否定义了成员,或者应该是:

    message.member.addRole(role).catch(console.log);
    

    编辑:

    尝试使用:

    if(!member.roles.has(role.id)) await member.addRole(role.id).catch(console.log);
    

    【讨论】:

    猜你喜欢
    • 2021-07-10
    • 1970-01-01
    • 2021-01-04
    • 2020-07-14
    • 1970-01-01
    • 2020-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多