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
d.js toolkit
d.js toolkit7mo 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!
Sky
SkyOP7mo ago
The frustrating part is that it works perfectly, you just need to input the password twice...
d.js docs
d.js docs7mo ago
To share long code snippets, use a service like gist, sourcebin, pastebin, or similar instead of posting them as large code blocks or files.
NyR
NyR7mo ago
^ it's hard to read with that much comments, and v10 what? djs v10 or api?
duck
duck7mo ago
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 fetch
Sky
SkyOP7mo ago
Oh 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);
NyR
NyR7mo ago
message.member is already the member, so you do not need to get it from the cache
Sky
SkyOP7mo ago
I'm using message.author?
NyR
NyR7mo ago
message.member will be GuildMember instance of the author, so you don't need to get/fetch it
Sky
SkyOP7mo ago
..?
NyR
NyR7mo ago
const member = message.member That's all you need to do
Sky
SkyOP7mo ago
fair 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)
NyR
NyR7mo ago
Wait, so the message is is dm?
Sky
SkyOP7mo ago
yes if (message.channel.type == ChannelType.DM)
NyR
NyR7mo ago
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(...)
Sky
SkyOP7mo ago
that caches the roles too?
NyR
NyR7mo ago
Yes
Sky
SkyOP7mo ago
const member = guild.members.fetch(message.author.id); ? still undefined :/
NyR
NyR7mo ago
Undefined or something else? fetch returns a promise, you need to resolve it first (await)
Sky
SkyOP7mo ago
undefined
TypeError: Cannot read properties of undefined (reading 'cache')
at Client.<anonymous> (D:\projects\BFIG Bot\Questbot\index.js:70:38)
TypeError: Cannot read properties of undefined (reading 'cache')
at Client.<anonymous> (D:\projects\BFIG Bot\Questbot\index.js:70:38)
but ofc when I send the password again, it's fine
NyR
NyR7mo ago
Again, resolve the promise of the fetch
Sky
SkyOP7mo ago
ah, thank you! now to make sure this only happens once per user and it's good to go for tomorrow 😄
Want results from more Discord servers?
Add your server