can't set role to user (user.roles.add is not a function)

If I have a code like this
const member = interaction.options.getMember("user");
const type = interaction.options.getRole("type");
member.roles.add(type);
const member = interaction.options.getMember("user");
const type = interaction.options.getRole("type");
member.roles.add(type);
It doesn't work and it shows that add() is not a member of roles And these are all the functions that are listed for roles
[[Prototype]] = Object
__defineGetter__ = ƒ __defineGetter__()
__defineSetter__ = ƒ __defineSetter__()
__lookupGetter__ = ƒ __lookupGetter__()
__lookupSetter__ = ƒ __lookupSetter__()
constructor = ƒ Object()
hasOwnProperty = ƒ hasOwnProperty()
isPrototypeOf = ƒ isPrototypeOf()
propertyIsEnumerable = ƒ propertyIsEnumerable()
toLocaleString = ƒ toLocaleString()
toString = ƒ toString()
valueOf = ƒ valueOf()
[[Prototype]] = Object
__defineGetter__ = ƒ __defineGetter__()
__defineSetter__ = ƒ __defineSetter__()
__lookupGetter__ = ƒ __lookupGetter__()
__lookupSetter__ = ƒ __lookupSetter__()
constructor = ƒ Object()
hasOwnProperty = ƒ hasOwnProperty()
isPrototypeOf = ƒ isPrototypeOf()
propertyIsEnumerable = ƒ propertyIsEnumerable()
toLocaleString = ƒ toLocaleString()
toString = ƒ toString()
valueOf = ƒ valueOf()
And that should be the correct way to set a role to a member according to the discord.js guide https://discordjs.guide/popular-topics/faq.html#how-do-i-add-a-role-to-a-guild-member instead I get this error:
error: Uncaught TypeError: member.roles.add is not a function
member.roles.add(type);
^
error: Uncaught TypeError: member.roles.add is not a function
member.roles.add(type);
^
discord.js: v14.16.3 deno: v2.0.3
discord.js Guide
Imagine a guide... that explores the many possibilities for your discord.js bot.
10 Replies
d.js toolkit
d.js toolkit2d ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
d.js docs
d.js docs2d ago
:discord: Guild Resource - Add Guild Member Role PUT /guilds/guild.id/members/user.id/roles/role.id Adds a role to a guild member. Requires the MANAGE_ROLES permission. Returns a 204 empty response on success. Fires a Guild Member Update Gateway event. read more
treble/luna
treble/luna2d ago
log user, and consider properly naming your variables, a user isnt a member
cats
catsOP2d ago
I understand, It was just an example. Ok, I noticed that it returns False when interaction.inCachedGuild() is called, and from what I understand it needs to be cached for it to work correctly. How can this be achieved?
treble/luna
treble/luna2d ago
By having the Guilds intent
cats
catsOP2d ago
it should be like this right? that's my current setup
import { Client, GatewayIntentBits } from 'npm:discord.js';

const client = new Client({ intents: [GatewayIntentBits.Guilds] });
import { Client, GatewayIntentBits } from 'npm:discord.js';

const client = new Client({ intents: [GatewayIntentBits.Guilds] });
treble/luna
treble/luna2d ago
Is your app a user installed app? If not, is it added with the bot scope
cats
catsOP2d ago
it's not a user installed app, and sorry where to check if it's added to the bot scope or not?
treble/luna
treble/luna2d ago
When you added the bot to your server
cats
catsOP2d ago
Oh ok, Thanks that fixed it, forgot to add it in the dev portal
Want results from more Discord servers?
Add your server