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 toolkit•6mo 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
SkyOP•6mo ago
The frustrating part is that it works perfectly, you just need to input the password twice...
d.js docs
d.js docs•6mo 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
NyR•6mo ago
^ it's hard to read with that much comments, and v10 what? djs v10 or api?
duck
duck•6mo 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
SkyOP•6mo 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
NyR•6mo ago
message.member is already the member, so you do not need to get it from the cache
Sky
SkyOP•6mo ago
I'm using message.author?
NyR
NyR•6mo ago
message.member will be GuildMember instance of the author, so you don't need to get/fetch it
Sky
SkyOP•6mo ago
..?
NyR
NyR•6mo ago
const member = message.member That's all you need to do
Sky
SkyOP•6mo 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
NyR•6mo ago
Wait, so the message is is dm?
Sky
SkyOP•6mo ago
yes if (message.channel.type == ChannelType.DM)
NyR
NyR•6mo 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
SkyOP•6mo ago
that caches the roles too?
NyR
NyR•6mo ago
Yes
Sky
SkyOP•6mo ago
const member = guild.members.fetch(message.author.id); ? still undefined :/
NyR
NyR•6mo ago
Undefined or something else? fetch returns a promise, you need to resolve it first (await)
Sky
SkyOP•6mo 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
NyR•6mo ago
Again, resolve the promise of the fetch
Sky
SkyOP•6mo 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