Feature/4 add data delete on guild remove #7

Merged
AstroGD merged 7 commits from feature/4-add-data-delete-on-guild-remove into dev 2022-11-25 18:31:44 +01:00
3 changed files with 48 additions and 1 deletions
Showing only changes of commit 8b72326419 - Show all commits

View File

@ -1,3 +1,4 @@
import { runCleanup } from "../service";
import client from "./index"; import client from "./index";
const token = process.env["TOKEN"]; const token = process.env["TOKEN"];
@ -7,4 +8,6 @@ client.login(token);
client.on("ready", () => { client.on("ready", () => {
console.log(`Connected to Discord API. Bot account is ${client.user?.tag} (${client.user?.id})`); console.log(`Connected to Discord API. Bot account is ${client.user?.tag} (${client.user?.id})`);
runCleanup();
}); });

39
src/service/cleanup.ts Normal file
View File

@ -0,0 +1,39 @@
import { Badword, database, GuildSetting } from "../data";
import client from "../client";
import { DiscordAPIError } from "discord.js";
export default async function execute(): Promise<void> {
const guilds = await database.getRepository(GuildSetting).find({
where: {
preserveDataOnGuildLeave: false
}
});
for (const guild of guilds) {
try {
await client.guilds.fetch(guild.id);
} catch (error) {
if (!(error instanceof DiscordAPIError)) {
console.error(`service.cleanup failed: ${error}`);
return;
}
const id = guild.id;
// 5001 = Missing access
if (error.code.toString() !== "50001") {
console.warn(`Guild ${guild.id} is unavailable but not because of error 5001:\n${error}`);
continue;
}
await database.getRepository(Badword).delete({
guildID: guild.id
});
await database.getRepository(GuildSetting).remove(guild);
console.log(`Removed data for guild ${id}`);
}
}
console.log("Cleanup completed");
}

View File

@ -1 +1,6 @@
import "./uptime"; import "./uptime";
import runCleanup from "./cleanup";
export {
runCleanup
}