Search code examples
javascriptnode.jsdiscorddiscord.js

Fetch overwrites completely different constant


I really had difficulties finding the appropriate title, I'm not even sure why or how this is happening, maybe someone can help me out here, I think I'm just not seeing something.
I created an invite tracker for my Discord Bot that should work in theory. I have a cache with all the invites before a member joined, and I'm getting the new invites after a member joined to see which invite has gained a use.

Here's my code in the guildMemberAdd event:

//main.js: client.invites = new Discord.Collection();

async function getInvite() {
  if(!member.guild.members.me.permissions.has(Discord.PermissionFlagsBits.ManageGuild)) return false;

  const invites_cache = client.invites.get(member.guild.id);

  console.log(invites_cache.map(i => `${i.code}: ${i.uses}`)); //Output: ['XY: 1', 'YZ: 3' ]

  if(!invites_cache) { client.invites.set(member.guild.id, invites_new); return null; }
  const invites_new = await member.guild.invites.fetch();
            
  console.log(invites_cache.map(i => `${i.code}: ${i.uses}`)); //Output: ['XY: 2', 'YZ: 3' ]
  console.log(invites_new.map(i => `${i.code}: ${i.uses}`)); //Output: ['XY: 2', 'YZ: 3' ]

  const invite = invites_new.find(inv => invites_cache.find(i => i.code === inv.code).uses < inv.uses) || null;
  client.invites.set(member.guild.id, invites_new);

   return invite;
}

const invite = await getInvite();

The problem is that those two console logs return the exact same set of invites, with the exact same uses. What makes this weird though is the invite_cache log above the const invites_new = await member.guild.invites.fetch(); gets me the exact result I want, meaning that one of the invites has less uses. I included example outputs in the code block

How is this possible? Why does me fetching the current invites and assigning them to a different constant affect my other constant? Maybe I'm just missing something really obvious .

Thanks for any help in advance!

EDIT: I deleted one of the invites and it correctly showed me one log with and the new log without the invite. What still remained the same was the amount of uses the other invites had even though one of them should have increased by one. This is incredibly confusing to me, and I hope someone has an answer.


Solution

  • I found out why. Now that I see it, I am almost embarrassed that I haven't thought of it before. My ready event used to look like this:

    client.guilds.cache.forEach(guild => {
       if(!guild.members.me.permissions.has(Discord.PermissionFlagsBits.ManageGuild)) return;
       guild.invites.fetch().then(invites => { 
           client.invites.set(guild.id, invites); 
       }).catch(e => { console.log(e) })
    })
    

    I have realized now that apparently setting the actual invites leads to them updating themselves. Not sure if that is the actual reason but I came up with this solution that works perfectly fine now:

    client.guilds.cache.forEach(guild => {
       if(!guild.members.me.permissions.has(Discord.PermissionFlagsBits.ManageGuild)) return;
       guild.invites.fetch().then(invites_new => {
           const invites = new Array(); 
           invites_new.forEach(i => invites.push({ code: i.code, uses: i.uses, inviter: i.inviter }));
           client.invites.set(guild.id, invites); 
       }).catch(e => { console.log(e) })
    })