Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Purge configuration data of removed guilds #39

Merged
merged 3 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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