Skip to content

Commit

Permalink
✨ Purge configuration data of removed guilds (#39)
Browse files Browse the repository at this point in the history
* ✨ Delete configs of removed guilds

* Move deletion to configHelpers

* Move logging to reset function

Co-authored-by: MarcusOtter <[email protected]>
  • Loading branch information
n1ckoates and MarcusOtter authored Jan 31, 2022
1 parent 530b4e8 commit 32b90b7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion src/helpers/configHelpers.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
})();
Expand Down

0 comments on commit 32b90b7

Please sign in to comment.