Home > front end >  TypeError: Cannot read property 'fetch' of undefined Discord.js for Invite Logger
TypeError: Cannot read property 'fetch' of undefined Discord.js for Invite Logger

Time:10-25

This is the code which I am currently using for the Invite Logger and when I run it, it shows an error stating that it cannot read the 'fetch' property.

const invites = new Map();
const wait = require("timers/promises").setTimeout;

client.on("ready", async() => {
  await wait(1000);

  client.guilds.cache.forEach(async(guild) => {
    const firstInvites = await guild.invites.fetch();
    invites.set(guild.id, new Map(firstInvites.map((invite) => [invite.code, invite.uses])));
  });
});

client.on("inviteDelete", (invite) => {
  invites.get(invite.guild.id).delete(invite.code);
});

client.on("inviteCreate", (invite) => {
  invites.get(invite.guild.id).set(invite.code, invite.uses);
});

client.on("guildMemberAdd", member => {
  member.guild.invites.fetch().then(newInvites => {
    const oldInvites = invites.get(member.guild.id);
    const invite = newInvites.find(i => i.uses > oldInvites.get(i.code));
    const inviter = client.users.cache.get(invite.inviter.id);
    const logChannel = member.guild.channels.cache.find(channel => channel.name === "bot-testing");
    inviter
      ?
      logChannel.send(`${member.user.tag} joined using invite code ${invite.code} from ${inviter.tag}. Invite was used ${invite.uses} times since its creation.`) :
      logChannel.send(`${member.user.tag} joined but I couldn't find through which invite.`);
  });
});
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

This is the error message :

enter image description here

I am using Discord.js version 12.5.3 It would be great if someone can help me out :D

CodePudding user response:

On v12.5.3, there is no https://discord.js.org/#/docs/main/12.5.3/class/Guild?scrollTo=available

    const firstInvites = await guild.fetchInvites();
// ...
    member.guild.fetchInvites().then(newInvites => {
  • Related