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)
After looking through the Discord.js classes, I found out that you cannot simply use
let member = interaction.member;
to get a guild member, but instead, you have to do
let member = await guild.members.fetch("ID");
After doing that, the member.permissions.has, member.roles.add and member.roles.remove functions all work!
The only function that still doesn't work is the member.roles.some, this is because the GuildMemberRoleManager class only has add, set, and remove functions. The member.roles.some function can be changed to use member._roles as it returns a list of Strings containing all the roles the guild member has and can be done as such:
function hasRole(member, roleID) {
return member._roles.indexOf(roleID) >= 0;
}
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.fetch();
return roles.some(role => role.id === roleID);
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)