Constant holding a collection of invites changing without being changed

I explained exactly what's going on in the comments of the code below, any help would be greatly appreciated.
client.on("guildMemberAdd", async (member) => {
let guild = member.guild
const oldInvites = await (Invites.get(guild.id) || new Collection()).clone()
console.log(oldInvites) // Logs old invites
console.log(Invites.get(guild.id)) // Logs old invites
const newInvites = await guild.invites.fetch().catch(() => { });
console.log(oldInvites) // Logs new invites for some reason???
console.log(Invites.get(guild.id)) // Logs new invites for some reason???
console.log(newInvites) // Logs new invites
})
client.on("guildMemberAdd", async (member) => {
let guild = member.guild
const oldInvites = await (Invites.get(guild.id) || new Collection()).clone()
console.log(oldInvites) // Logs old invites
console.log(Invites.get(guild.id)) // Logs old invites
const newInvites = await guild.invites.fetch().catch(() => { });
console.log(oldInvites) // Logs new invites for some reason???
console.log(Invites.get(guild.id)) // Logs new invites for some reason???
console.log(newInvites) // Logs new invites
})
18 Replies
Kinect3000
Kinect30002y ago
It looks like ur storing the reference to the Invite objects, which is mutated upon fetching them
RobbyV
RobbyV2y ago
What do you mean? The Invites constant is just initiated as const Invites = new Collection(); at the top of the code.
Kinect3000
Kinect30002y ago
Are you storing Invite objects in there?
RobbyV
RobbyV2y ago
I am lower down in the code
Kinect3000
Kinect30002y ago
Or more so, Collection of Invite objects
RobbyV
RobbyV2y ago
Basically right after the code above goes this code:
let invite = await newInvites.find(_i => oldInvites.has(_i.code) && oldInvites.get(_i.code).uses < _i.uses) || oldInvites.find(_i => !newInvites.has(_i.code)) || guild.vanityURLCode;
await Invites.set(guild.id, newInvites);
let invite = await newInvites.find(_i => oldInvites.has(_i.code) && oldInvites.get(_i.code).uses < _i.uses) || oldInvites.find(_i => !newInvites.has(_i.code)) || guild.vanityURLCode;
await Invites.set(guild.id, newInvites);
Kinect3000
Kinect30002y ago
Yea, ur storing the Invite objects
RobbyV
RobbyV2y ago
So it's basically supposed to compare old invites and new invites before updating the collection
Kinect3000
Kinect30002y ago
D.js mutates those Invite object if your refetch
RobbyV
RobbyV2y ago
What do you mean by mutate?
Kinect3000
Kinect30002y ago
Update in place Like how Arrays are mutable
RobbyV
RobbyV2y ago
How do I keep the old collection the same then? Because this never happened with d.js 12
Kinect3000
Kinect30002y ago
You could map it to a plain object or ur own class w/ just the uses and inviter And whatever else you need
RobbyV
RobbyV2y ago
Cloning the collection doesn't help? I probably sound stupid but how do I map it to a plain object?
Kinect3000
Kinect30002y ago
It’s a shallow copy The items are basically moved (but not actually deleted) over to a new Collection. It doesn’t deep copy the items .map((item) => ({ key: val, key2, val2, … })
RobbyV
RobbyV2y ago
I have to loop through and map every single invite??
Kinect3000
Kinect30002y ago
Yea You make it sound like a big issue
RobbyV
RobbyV2y ago
I do lol, let me try that. Alright, it worked, thanks a lot for your help.