V10, roles cache pulling only "undefined" roles until the code is ran again
yes I know I need to update, I am not learning multiple versions worth of things in a single day, I just need to tweak my bot from last year to do a small additional thing for in a couple days time.
I had this issue when I was originally making this bot, but I don't remember what fixed it, and I sure as hell haven't changed anything that could have been for it. Essentially this bot checks for a password via a DM, then gives a role to that user/checks if they have the role already. The issue is that the bot is throwing "TypeError: Cannot read properties of undefined (reading 'roles')" on the first input of the password, but on the second it runs fine. Code attached
Line 69 has the issue, the first if statement part just checking if the user has the first role
22 Replies
- 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!The frustrating part is that it works perfectly, you just need to input the password twice...
^ it's hard to read with that much comments, and v10 what? djs v10 or api?
if (member.roles.cache.has(roleL1ID)) {
on the assumption this is the erroring line:
1. I'd guess v10 api, their code seems to be for v14 of discord.js
2. members aren't always cached, so you'd want to fetch those
guilds however are cached on ready since you have the Guilds
intent, so that's something you don't need to fetchOh rip is it 14? Apologies, I've not touched this in basically exactly a year and just saw "V10" in the file 😅
I'd need to fetch members? HM, I've not touched any of that part of the code 🤔
I already have
let member = guild.members.cache.get(message.author.id);
message.member
is already the member, so you do not need to get it from the cacheI'm using message.author?
message.member
will be GuildMember instance of the author, so you don't need to get/fetch it..?
const member = message.member
That's all you need to dofair
but how do I make sure the roles are cached then at the start and not after the command fails once?
(or well, right after the DM is sent so it can grab the right roles from the right person)
Wait, so the message is is dm?
yes
if (message.channel.type == ChannelType.DM)
Then yeah, message.member will not be present, get the member like how you were doing but instead of cache, fetch them, as all members are not cached
guild.members.fetch(...)
that caches the roles too?
Yes
const member = guild.members.fetch(message.author.id);
?
still undefined :/Undefined or something else? fetch returns a promise, you need to resolve it first (await)
undefined
but ofc when I send the password again, it's fine
Again, resolve the promise of the fetch
ah, thank you!
now to make sure this only happens once per user and it's good to go for tomorrow 😄