Search code examples
javascriptnode.jsdiscorddiscord.jsroles

Recieving a TypeError (Not a Role/Snowflake) in Discord.js


Some of what I've seen/tried:

I'm trying to create a cosmetic roles section in my bot. My current code is:

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

My previous code was the same except without the .join(" "). I get the following error:

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

The "Created new role" comes from the line that creates it. Note that this message is sent with a .then on my role creation script. How do I get this to work? Note that it creates my role just fine, it just can't add it to the user executing the command. Using await does not work as addRole is not an async function.

Update: I've figured out how to do it: use a second .then that passes the role.id. Thank you for your suggestions!


Solution

  • Is member defined, or should that be:

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

    EDITED:

    Try using:

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