From 32b90b72725f910d4d77e76412abcccb5e3c24a8 Mon Sep 17 00:00:00 2001 From: Nicholas Christopher Date: Mon, 31 Jan 2022 06:38:56 -0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Purge=20configuration=20data=20of?= =?UTF-8?q?=20removed=20guilds=20(#39)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✨ Delete configs of removed guilds * Move deletion to configHelpers * Move logging to reset function Co-authored-by: MarcusOtter --- src/helpers/configHelpers.ts | 18 +++++++++++++++++- src/index.ts | 8 ++++++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/helpers/configHelpers.ts b/src/helpers/configHelpers.ts index 747d02ef..0f46c2dc 100644 --- a/src/helpers/configHelpers.ts +++ b/src/helpers/configHelpers.ts @@ -1,4 +1,4 @@ -import { Guild } from "discord.js"; +import { Client, Guild } from "discord.js"; import * as defaultConfig from "../config.json"; import { resolve as pathResolve } from "path"; import * as fs from "fs"; @@ -76,9 +76,25 @@ export function resetConfigToDefault(guildId: string): boolean { if (!fs.existsSync(path)) return false; fs.rmSync(path); guildConfigsCache.delete(guildId); + console.log(`Deleted data for guild ${guildId}`); return true; } +export function deleteConfigsFromUnkownServers(client: Client): void { + if (!client.guilds.cache.size) { + console.warn("No guilds available; skipping config deletion."); + return; + } + + const configFiles = fs.readdirSync(CONFIGS_PATH); + configFiles.forEach(file => { + const guildId = file.split(".")[0]; + if (!client.guilds.cache.has(guildId)) { + resetConfigToDefault(guildId); + } + }); +} + function readConfigFromFile(guildId: string): NeedleConfig | undefined { const path = getGuildConfigPath(guildId); if (!fs.existsSync(path)) return undefined; diff --git a/src/index.ts b/src/index.ts index 8e24b7db..cfcf52ec 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import { Client, Intents } from "discord.js"; import { getOrLoadAllCommands } from "./handlers/commandHandler"; import { handleInteractionCreate } from "./handlers/interactionHandler"; import { handleMessageCreate } from "./handlers/messageHandler"; -import { getApiToken } from "./helpers/configHelpers"; +import { deleteConfigsFromUnkownServers, getApiToken, resetConfigToDefault } from "./helpers/configHelpers"; (async () => { (await import("dotenv")).config(); @@ -11,10 +11,14 @@ import { getApiToken } from "./helpers/configHelpers"; await getOrLoadAllCommands(false); const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] }); - CLIENT.once("ready", () => console.log("Ready!")); + CLIENT.once("ready", () => { + console.log("Ready!"); + deleteConfigsFromUnkownServers(CLIENT); + }); CLIENT.on("interactionCreate", interaction => handleInteractionCreate(interaction).catch(e => console.log(e))); CLIENT.on("messageCreate", message => handleMessageCreate(message).catch(e => console.log(e))); + CLIENT.on("guildDelete", guild => { resetConfigToDefault(guild.id); }); CLIENT.login(getApiToken() ?? undefined); })();