Caching Issues

Hello! I'm fairly new to discord.js and I have some issued with this code I wrote.
if (oldMember.partial) await oldMember.fetch();
if (newMember.partial) await newMember.fetch();

const oldRoles = oldMember.roles.cache.has(checkedRoleId);
const newRoles = newMember.roles.cache.has(checkedRoleId);
if (oldMember.partial) await oldMember.fetch();
if (newMember.partial) await newMember.fetch();

const oldRoles = oldMember.roles.cache.has(checkedRoleId);
const newRoles = newMember.roles.cache.has(checkedRoleId);
I keep getting this error: TypeError: Cannot read properties of undefined (reading 'cache')
33 Replies
d.js toolkit
d.js toolkit3w 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!
Amgelo
Amgelo3w ago
what is oldMember and newMember?
ass man 🧍
ass man 🧍OP3w ago
Sorry for not specifying, this is a guildMemberUpdate event.
Amgelo
Amgelo3w ago
btw it doesn't really make sense to fetch your oldMember when you have a newMember fetched, fetching returns the current data your current data is already your newMember fetching won't return past data
ass man 🧍
ass man 🧍OP3w ago
So would I just remove the oldMember.fetch part and leave the newMember or remove both?
Amgelo
Amgelo3w ago
what do you want to do?
ass man 🧍
ass man 🧍OP3w ago
Let me remove both and get back to you. It still says the same error.
Amgelo
Amgelo3w ago
that doesn't answer my question
ass man 🧍
ass man 🧍OP3w ago
Oh like what am I trying to do?
Amgelo
Amgelo3w ago
yeah in general what are you trying to achieve
ass man 🧍
ass man 🧍OP3w ago
I'm trying to check to see if a certain role got removed, and if so, it removes a different role.
Amgelo
Amgelo3w ago
I think you'd need to fetch all members beforehand or at least the ones you want to track the oldMember relies on cache discord only emits the guildMemberUpdate event when the member changes, but it doesn't tell what changed or what was its old state partials allow you to receive the event even without the member being cached, but there's pretty much no data you can get from that old state, since you don't have it partial objects pretty much only have ids
ass man 🧍
ass man 🧍OP3w ago
ok ok so would I fetch all members in the same file before everything or would i do it in lets say in a ready event
Amgelo
Amgelo3w ago
in ready you can't do it on that event, at that point the member has already changed
ass man 🧍
ass man 🧍OP3w ago
okay makes sense
Amgelo
Amgelo3w ago
that's why this that error is kinda weird though, I don't think it's coming from that codeblock because GuildMember#roles is always present
ass man 🧍
ass man 🧍OP3w ago
It is there when I logged it heres the full error
TypeError: Cannot read properties of undefined (reading 'cache')
at Object.execute (C:\Users\nbarb\psrp\src\events\5050System.js:10:40)
at Client.<anonymous> (C:\Users\nbarb\psrp\src\utils\EventHandler.js:113:21)
at Client.emit (node:events:519:28)
at GuildMemberUpdateAction.handle (C:\Users\nbarb\psrp\node_modules\discord.js\src\client\actions\GuildMemberUpdate.js:30:74)
at module.exports [as GUILD_MEMBER_UPDATE] (C:\Users\nbarb\psrp\node_modules\discord.js\src\client\websocket\handlers\GUILD_MEMBER_UPDATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\nbarb\psrp\node_modules\discord.js\src\client\websocket\WebSocketManager.js:348:31)
at WebSocketManager.<anonymous> (C:\Users\nbarb\psrp\node_modules\discord.js\src\client\websocket\WebSocketManager.js:232:12)
at WebSocketManager.emit (C:\Users\nbarb\psrp\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:287:31)
at WebSocketShard.<anonymous> (C:\Users\nbarb\psrp\node_modules\@discordjs\ws\dist\index.js:1190:51)
at WebSocketShard.emit (C:\Users\nbarb\psrp\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:287:31)
at WebSocketShard.onMessage (C:\Users\nbarb\psrp\node_modules\@discordjs\ws\dist\index.js:1005:14)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
TypeError: Cannot read properties of undefined (reading 'cache')
at Object.execute (C:\Users\nbarb\psrp\src\events\5050System.js:10:40)
at Client.<anonymous> (C:\Users\nbarb\psrp\src\utils\EventHandler.js:113:21)
at Client.emit (node:events:519:28)
at GuildMemberUpdateAction.handle (C:\Users\nbarb\psrp\node_modules\discord.js\src\client\actions\GuildMemberUpdate.js:30:74)
at module.exports [as GUILD_MEMBER_UPDATE] (C:\Users\nbarb\psrp\node_modules\discord.js\src\client\websocket\handlers\GUILD_MEMBER_UPDATE.js:4:36)
at WebSocketManager.handlePacket (C:\Users\nbarb\psrp\node_modules\discord.js\src\client\websocket\WebSocketManager.js:348:31)
at WebSocketManager.<anonymous> (C:\Users\nbarb\psrp\node_modules\discord.js\src\client\websocket\WebSocketManager.js:232:12)
at WebSocketManager.emit (C:\Users\nbarb\psrp\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:287:31)
at WebSocketShard.<anonymous> (C:\Users\nbarb\psrp\node_modules\@discordjs\ws\dist\index.js:1190:51)
at WebSocketShard.emit (C:\Users\nbarb\psrp\node_modules\@vladfrangu\async_event_emitter\dist\index.cjs:287:31)
at WebSocketShard.onMessage (C:\Users\nbarb\psrp\node_modules\@discordjs\ws\dist\index.js:1005:14)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
Amgelo
Amgelo3w ago
and which one is line 10 in 5050System.js
ass man 🧍
ass man 🧍OP3w ago
const oldRoles = oldMember.roles.cache.has(checkedRoleId);
Amgelo
Amgelo3w ago
maybe the underlying partial isn't being patched when you fetch well regardless, you won't get that error anymore because your approach will change entirely
ass man 🧍
ass man 🧍OP3w ago
okay ty hey so uhm, im still getting the same error after fetching all the members in the guild
Amgelo
Amgelo3w ago
show your current code
ass man 🧍
ass man 🧍OP3w ago
const guildId = '1302367535185920131';

module.exports = {
event: 'ready',
once: false,

async execute(client) {
const guild = client.guilds.cache.get(guildId);

try {
await guild.members.fetch();
console.log(`Got all members`);
} catch (error) {
console.error('Failed to fetch members', error);
}
}
}
const guildId = '1302367535185920131';

module.exports = {
event: 'ready',
once: false,

async execute(client) {
const guild = client.guilds.cache.get(guildId);

try {
await guild.members.fetch();
console.log(`Got all members`);
} catch (error) {
console.error('Failed to fetch members', error);
}
}
}
Amgelo
Amgelo3w ago
and the one that errors aka 5050System.js
ass man 🧍
ass man 🧍OP3w ago
const checkedRoleId = '1302367535395508325';
const secondaryRoleId = '1306786490671562833';

module.exports = {
event: 'guildMemberUpdate',
once: false,

async execute(oldMember, newMember, client) {
try {
const oldRoles = oldMember.roles.cache.has(checkedRoleId);
const newRoles = newMember.roles.cache.has(checkedRoleId);

if (oldRoles && !newRoles) {
if (newMember.roles.cache.has(secondaryRoleId)) {
await newMember.roles.remove(secondaryRoleId);
}
}
} catch (error) {
console.error(error);
}
}
};
const checkedRoleId = '1302367535395508325';
const secondaryRoleId = '1306786490671562833';

module.exports = {
event: 'guildMemberUpdate',
once: false,

async execute(oldMember, newMember, client) {
try {
const oldRoles = oldMember.roles.cache.has(checkedRoleId);
const newRoles = newMember.roles.cache.has(checkedRoleId);

if (oldRoles && !newRoles) {
if (newMember.roles.cache.has(secondaryRoleId)) {
await newMember.roles.remove(secondaryRoleId);
}
}
} catch (error) {
console.error(error);
}
}
};
Amgelo
Amgelo3w ago
sounds like a missmatch
ass man 🧍
ass man 🧍OP3w ago
wdym?
d.js docs
d.js docs3w ago
The order of function parameters must match between definition and function call.
function execute(client, interaction) { ... };
execute(interaction, client);
function execute(client, interaction) { ... };
execute(interaction, client);
- mismatch! you pass an interaction where the client is expected - mismatch! you pass the client where an interaction is expected
ass man 🧍
ass man 🧍OP3w ago
let me log oldMember since thats the one thats erroring
Amgelo
Amgelo3w ago
in case you're appending/prepending a client to your event handlers, don't every djs object has a client property eg
d.js docs
d.js docs3w ago
:property: GuildMember#client @14.16.3 The client that instantiated this
ass man 🧍
ass man 🧍OP3w ago
oh man i feel so stupid 😭
Amgelo
Amgelo3w ago
dw, it's a common thing that a lot of people encounter
Want results from more Discord servers?
Add your server