- Fetch invites every 5 minutes

- Add fail-safe to sending join messages (Don't want that happening again)
This commit is contained in:
Christopher Cookman 2024-06-28 23:51:07 -06:00
parent 24ce1d2e96
commit 795d75655c
Signed by: ChrisChrome
GPG key ID: A023A26E42C33A42

View file

@ -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,