feat: clean up data on bot and member leaves

This commit is contained in:
Naomi Carrigan 2024-07-07 13:03:34 -07:00
parent 76cc030534
commit bcdcf4629a
Signed by: naomi
SSH Key Fingerprint: SHA256:rca1iUI2OhAM6n4FIUaFcZcicmri0jgocqKiTTAfrt8
4 changed files with 42 additions and 5 deletions

View File

@ -1,6 +1,7 @@
import { Guild } from "discord.js";
import { ExtendedClient } from "../../interfaces/ExtendedClient";
import { errorHandler } from "../../utils/errorHandler";
/**
*
* @param {ExtendedClient} bot The bot's Discord instance.
@ -10,7 +11,18 @@ export const onGuildDelete = async function (
bot: ExtendedClient,
guild: Guild
) {
await bot.env.debugHook.send({
content: `LEFT GUILD: ${guild.name} (${guild.id}) `
});
try {
await bot.env.debugHook.send({
content: `LEFT GUILD: ${guild.name} (${guild.id}) `
});
await bot.db.cases.deleteMany({ where: { serverId: guild.id } });
await bot.db.levelRoles.deleteMany({ where: { serverId: guild.id } });
await bot.db.levels.deleteMany({ where: { serverId: guild.id } });
await bot.db.configs.deleteMany({ where: { serverId: guild.id } });
await bot.db.roles.deleteMany({ where: { serverId: guild.id } });
await bot.db.birthdays.deleteMany({ where: { serverId: guild.id } });
await bot.db.security.deleteMany({ where: { serverId: guild.id } });
} catch (err) {
await errorHandler(bot, "on guild delete", err);
}
};

View File

@ -25,6 +25,11 @@ export const onMemberRemove = async (
serverId_userId: { serverId: guild.id, userId: user.id }
}
});
await bot.db.levels.delete({
where: {
serverId_userId: { serverId: guild.id, userId: user.id }
}
});
if (!config.eventLogChannel) {
return;

View File

@ -1,4 +1,5 @@
import { ExtendedClient } from "../interfaces/ExtendedClient";
import { checkEntitledGuild } from "../utils/checkEntitledGuild";
import { errorHandler } from "../utils/errorHandler";
/**
@ -12,6 +13,16 @@ export const maintainSecurity = async (bot: ExtendedClient) => {
const records = await bot.db.security.findMany();
const date = new Date(new Date().getTime() + 24 * 60 * 60 * 1000);
for (const record of records) {
const guild =
bot.guilds.cache.get(record.serverId) ||
(await bot.guilds.fetch(record.serverId).catch(() => null));
if (!guild) {
continue;
}
const isEntitled = await checkEntitledGuild(bot, guild);
if (!isEntitled) {
continue;
}
await fetch(
`https://discord.com/api/v10/guilds/${record.serverId}/incident-actions`,
{

View File

@ -1,4 +1,5 @@
import { ExtendedClient } from "../interfaces/ExtendedClient";
import { checkEntitledGuild } from "../utils/checkEntitledGuild";
import { errorHandler } from "../utils/errorHandler";
/**
@ -13,11 +14,19 @@ export const postBirthdays = async (bot: ExtendedClient) => {
const configs = await bot.db.configs.findMany();
const withChannel = configs.filter((c) => c.birthdayChannel);
for (const record of withChannel) {
const guild = bot.guilds.cache.get(record.serverId);
const guild =
bot.guilds.cache.get(record.serverId) ||
(await bot.guilds.fetch(record.serverId).catch(() => null));
if (!guild) {
continue;
}
const channel = guild.channels.cache.get(record.birthdayChannel);
const isEntitled = await checkEntitledGuild(bot, guild);
if (!isEntitled) {
continue;
}
const channel =
guild.channels.cache.get(record.birthdayChannel) ||
(await bot.guilds.fetch(record.birthdayChannel).catch(() => null));
if (!channel || !("send" in channel)) {
continue;
}