|
| 1 | +// Checks for any guilds that may have been added/removed while the bot was offline |
| 2 | +import { eq } from "drizzle-orm"; |
| 3 | + |
| 4 | +import { dbDiscordTable } from "../../db/schema"; |
| 5 | +import client from "../.."; |
| 6 | +import { db } from "../../db/db"; |
| 7 | +import { config } from "../../config"; |
| 8 | + |
| 9 | +export default async function () { |
| 10 | + console.log("Checking for guilds to update on startup..."); |
| 11 | + |
| 12 | + let currentGuilds: string[] = []; |
| 13 | + |
| 14 | + // Keep checking every 10 seconds until currentGuilds is not empty |
| 15 | + while (currentGuilds.length === 0) { |
| 16 | + console.log("Waiting for guilds to load..."); |
| 17 | + currentGuilds = client.guilds.cache.map((guild) => guild.id); |
| 18 | + if (currentGuilds.length === 0) { |
| 19 | + await new Promise((resolve) => |
| 20 | + setTimeout(resolve, config.discordWaitForGuildCacheTime), |
| 21 | + ); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + // Get all the guilds from the database |
| 26 | + const data = await db.select().from(dbDiscordTable); |
| 27 | + |
| 28 | + console.log( |
| 29 | + `Currently in ${currentGuilds.length} guilds, checking against ${data.length} in the database.`, |
| 30 | + ); |
| 31 | + |
| 32 | + // Find any guilds that are in the database but not in the current guilds |
| 33 | + const missingGuilds = data.filter( |
| 34 | + (guild) => !currentGuilds.includes(guild.guildId), |
| 35 | + ); |
| 36 | + |
| 37 | + // Find any guilds that are in the current guilds but not in the database |
| 38 | + const newGuilds = currentGuilds.filter( |
| 39 | + (id) => !data.some((guild) => guild.guildId === id), |
| 40 | + ); |
| 41 | + |
| 42 | + // Update the database for missing guilds |
| 43 | + try { |
| 44 | + await Promise.all( |
| 45 | + missingGuilds.map(async (guild) => { |
| 46 | + console.log(`Removing guild from tracking: ${guild.guildId}`); |
| 47 | + const result = await db |
| 48 | + .update(dbDiscordTable) |
| 49 | + .set({ isInServer: false }) |
| 50 | + .where(eq(dbDiscordTable.guildId, guild.guildId)) |
| 51 | + .returning(); |
| 52 | + |
| 53 | + if (result.length > 0) { |
| 54 | + console.log( |
| 55 | + `Successfully removed guild ${guild.guildId} from tracking.`, |
| 56 | + ); |
| 57 | + } else { |
| 58 | + console.error( |
| 59 | + `Failed to remove guild ${guild.guildId} from tracking.`, |
| 60 | + ); |
| 61 | + } |
| 62 | + }), |
| 63 | + ); |
| 64 | + } catch (error) { |
| 65 | + console.error("Error while removing missing guilds:", error); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + await Promise.all( |
| 70 | + newGuilds.map(async (guildId) => { |
| 71 | + console.log(`Adding new guild to tracking: ${guildId}`); |
| 72 | + const result = await db |
| 73 | + .insert(dbDiscordTable) |
| 74 | + .values({ |
| 75 | + guildId, |
| 76 | + allowedPublicSharing: false, |
| 77 | + feedrUpdatesChannelId: null, |
| 78 | + isInServer: true, |
| 79 | + memberCount: 0, |
| 80 | + }) |
| 81 | + .returning(); |
| 82 | + |
| 83 | + if (result.length > 0) { |
| 84 | + console.log( |
| 85 | + `Successfully added guild ${guildId} to tracking.`, |
| 86 | + ); |
| 87 | + } else { |
| 88 | + console.error( |
| 89 | + `Failed to add guild ${guildId} to tracking.`, |
| 90 | + ); |
| 91 | + } |
| 92 | + }), |
| 93 | + ); |
| 94 | + } catch (error) { |
| 95 | + console.error("Error while adding new guilds:", error); |
| 96 | + } |
| 97 | +} |
0 commit comments