eu.astrogd.white-leopard/src/service/cleanup.ts

39 lines
1.2 KiB
TypeScript

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");
}