From 795d75655c52c66a28997cfded603daa21f1f267 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Fri, 28 Jun 2024 23:51:07 -0600 Subject: [PATCH] - Fetch invites every 5 minutes - Add fail-safe to sending join messages (Don't want that happening again) --- index.js | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index e8fd454..6f6fa29 100644 --- a/index.js +++ b/index.js @@ -41,23 +41,28 @@ client.on('ready', async () => { client.invites = []; // Update Invites console.log(`${colors.cyan("[INFO]")} Fetching Invites...`); - await client.guilds.cache.forEach(guild => { //on bot start, fetch all guilds and fetch all invites to store - thisGuild = [] - guild.invites.fetch().then(guildInvites => { - guildInvites.each(guildInvite => { - client.invites[guildInvite.code] = guildInvite.uses - thisGuild.push(guildInvite.code) + const fetchInvites = () => { + await client.guilds.cache.forEach(guild => { //on bot start, fetch all guilds and fetch all invites to store + thisGuild = [] + guild.invites.fetch().then(guildInvites => { + guildInvites.each(guildInvite => { + client.invites[guildInvite.code] = guildInvite.uses + thisGuild.push(guildInvite.code) + }) + }).then(() => { + console.log(`${colors.cyan("[INFO]")} Fetched ${thisGuild.length} Invites for ${colors.green(guild.name)}`); }) - }).then(() => { - console.log(`${colors.cyan("[INFO]")} Fetched ${thisGuild.length} Invites for ${colors.green(guild.name)}`); + guild.fetchVanityData().then(vanityData => { + client.invites[vanityData.code] = vanityData.uses + }).catch(err => { + // Do nothing + }) + }) - guild.fetchVanityData().then(vanityData => { - client.invites[vanityData.code] = vanityData.uses - }).catch(err => { - // Do nothing - }) - - }) + } + fetchInvites(); + // Set interval to update invites every 5 minutes, just in case something desyncs + setInterval(fetchInvites, 300000); // Register commands @@ -152,9 +157,11 @@ client.on('guildMemberAdd', async (member) => { // We're just gonna always send client.channels.fetch(row.channel.toString()).then(channel => { if (!channel) return; // They probably set perms wrong member.guild.invites.fetch().then(async guildInvites => { //get all guild invites + sent = false; guildInvites.forEach(invite => { //basically a for loop over the invites if (invite.uses != client.invites[invite.code]) { //if it doesn't match what we stored: + if (sent) return console.log(`${colors.red("[ERROR]")} Something VERY bad has happened, and we seem to have lost track of invites.\nCurrent Invite Table:\n${JSON.stringify(client.invites, null, 2)}`); //if we already sent a message, this is a fail safe to prevent spam channel.send({ embeds: [{ color: 0x00ff00,