Feature/4 add data delete on guild remove #7
@ -1,3 +1,4 @@
|
||||
import { runCleanup } from "../service";
|
||||
import client from "./index";
|
||||
|
||||
const token = process.env["TOKEN"];
|
||||
@ -7,4 +8,6 @@ client.login(token);
|
||||
|
||||
client.on("ready", () => {
|
||||
console.log(`Connected to Discord API. Bot account is ${client.user?.tag} (${client.user?.id})`);
|
||||
|
||||
runCleanup();
|
||||
});
|
39
src/service/cleanup.ts
Normal file
39
src/service/cleanup.ts
Normal 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");
|
||||
}
|
@ -1 +1,6 @@
|
||||
import "./uptime";
|
||||
import runCleanup from "./cleanup";
|
||||
|
||||
export {
|
||||
runCleanup
|
||||
}
|
Loading…
Reference in New Issue
Block a user