Cannot read properties of undefined (reading `fetch`)

I'm trying to add a role to a newly joined user, after parsing their display name through an external API. However, I get an error stating that I cannot read the properties of undefined (reading fetch). const { Guild, GuildMember } = require('discord.js');
client.on(Events.GuildMemberAdd, async guildMember => {
await IsInOutfit(guildMember);
})
client.on(Events.GuildMemberAdd, async guildMember => {
await IsInOutfit(guildMember);
})
...
function AssignRole(guildMember, role)
{
guildMember.roles.add(Guild.roles.fetch(roles));
}
function AssignRole(guildMember, role)
{
guildMember.roles.add(Guild.roles.fetch(roles));
}
A quick note on GuildMember: according to VSCode, it's value is unused. I do not get why. Node version: v18.15.0
9 Replies
d.js toolkit
d.js toolkit11mo 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
chewie
chewie11mo ago
whatever Guild is, its undefined Or well, not undefined, but also not a proper guild instance what did you define roles as most likely you dont need to fetch the roles at all
HarleyErza
HarleyErzaOP11mo ago
I was attempting to get a list of roles from the guild, and sort through them by using the numerical ID. (I'm porting the bot from JavaCord, which is the way it used to work). Also, the fetch(roles); is most definetly a typo, as that should be role, refering to the numerical ID.
chewie
chewie11mo ago
then that would already not work because you didn't provide a role id to the function to begin with
FeroxNotMonday
FeroxNotMonday11mo ago
:skull:
chewie
chewie11mo ago
and then you could just do guildMember.roles.add(role)
HarleyErza
HarleyErzaOP11mo ago
Huh. That's a fair bit easier than expected. I'll give that a shot, and mark the post as resolved if that fixes it. Regardless, thank you for the help!
chewie
chewie11mo ago
:reallyFckingCuteCatThumbsUp:
HarleyErza
HarleyErzaOP11mo ago
Yep, that seems to have worked. I am missing an intent apparently, as the API throws a "Missing Access" error. But that's a separate issue.
Want results from more Discord servers?
Add your server