Home > Enterprise >  TypeError: member.roles.add is not a function (discord.js v13)
TypeError: member.roles.add is not a function (discord.js v13)

Time:02-19

I'm working on a bot that needs to check some things before giving/removing a role from a user, but for some reason, the .has(), .some(), .add() and, .remove() functions don't work.

Here's my code:

const Permissions = require('discord.js');

function isAdmin(member) {
    return member.permissions.has(Permissions.FLAGS.ADMINISTRATOR);
}

function hasRole(member, roleID) {
    return member.roles.some(role => role.id === roleID);
}

function giveRole(member, role) {
    member.roles.add(role)
}

function removeRole(member, role) {
    member.roles.remove(role);
}

Here's the full error that I get:

TypeError: member.roles.add is not a function
    at Object.giveRole (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\util.js:101:30)
    at Object.buttonClicked (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\commands\buttonRole\execute.js:23:14)
    at WebSocketManager.<anonymous> (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\index.js:101:28)
    at WebSocketManager.emit (node:events:390:28)
    at WebSocketShard.onMessage (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\node_modules\discord.js\src\client\websocket\WebSocketShard.js:300:54)
    at WebSocket.onMessage (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\node_modules\ws\lib\event-target.js:199:18)
    at WebSocket.emit (node:events:390:28)
    at Receiver.receiverOnMessage (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\node_modules\ws\lib\websocket.js:1137:20)
    at Receiver.emit (node:events:390:28)
    at Receiver.dataMessage (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\node_modules\ws\lib\receiver.js:528:14)
    at Receiver.getData (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\node_modules\ws\lib\receiver.js:446:17)
    at Receiver.startLoop (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\node_modules\ws\lib\receiver.js:148:22)
    at Receiver._write (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\node_modules\ws\lib\receiver.js:83:10)
    at writeOrBuffer (node:internal/streams/writable:389:12)
    at _write (node:internal/streams/writable:330:10)
    at Receiver.Writable.write (node:internal/streams/writable:334:10)
    at TLSSocket.socketOnData (C:\Users\Admin\OneDrive\Documents\Github Projects\discordBots-BOK-BOT\node_modules\ws\lib\websocket.js:1231:35)
    at TLSSocket.emit (node:events:390:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
    at TLSSocket.Readable.push (node:internal/streams/readable:228:10)
    at TLSWrap.onStreamRead (node:internal/stream_base_commons:199:23)

CodePudding user response:

In Discord.js version 12, the syntax mostly changed. The cache property now contains the cache for a lot of the roles, and there is a chance you need to use a Promise and fetch it...

// in an ASYNC FUNCTION
let roles = await member.roles.cache.fetch();
return roles.some(role => role.id === roleID);

CodePudding user response:

When doing anything with roles, and most stuff in general, you need to access it via the cache, for example:

  function hasRole(member, roleID) {
      return member.roles.cache.some(role => role.id === roleID);
  }

CodePudding user response:

Did you tried like this?

var role = message.guild.roles.cache.find(role => role.name === "Role_Name")
member.roles.add(role)

var role = message.guild.roles.cache.find(role => role.id === "Role_Id")
member.roles.add(role)
  • Related