Skip to content

Commit

Permalink
fix: run message delete timer every 1h
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Sep 16, 2023
1 parent 495a4c9 commit 8b95980
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/Utils/functions/timers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,28 @@ import { modActions } from '../../Scripts/networkLogs/modActions';
import { getDb } from './utils';

/** A function to start timers for blacklist expiry, messageData cleanup, etc. */
export default async function startTimers(client: Client) {
export default async function startTimers(client: Client<true>) {
const db = getDb();

// Delete all documents that are older than 24 hours old.
scheduleJob('messageExpired', { hour: 24, second: 5 }, async () => {
const olderThan = new Date(Date.now() - 60 * 60 * 24_000);
await db.messageData.deleteMany({ where: { timestamp: { lte: olderThan } } });
});
scheduleJob('invite/messageExpired', { hour: 1 }, async () => {
const olderThan1h = new Date(Date.now() - 60 * 60 * 1_000);
await db.hubInvites
.deleteMany({ where: { expires: { lte: olderThan1h } } })
.catch(() => null);

scheduleJob('inviteExpired', { hour: 1 }, async () => {
const olderThan = new Date(Date.now() - 60 * 60 * 1_000);
await db.hubInvites.deleteMany({ where: { expires: { lte: olderThan } } });
const olderThan24h = new Date(Date.now() - 60 * 60 * 24_000);
await db.messageData
.deleteMany({ where: { timestamp: { lte: olderThan24h } } })
.catch(() => null);
});

// Timers that start only if the bot is logged in.
const blacklistedServers = await db.blacklistedServers.findMany({ where: { expires: { isSet: true } } });
const blacklistedUsers = await db.blacklistedUsers.findMany({ where: { expires: { isSet: true } } });

// timer to unblacklist servers
blacklistedServers.forEach(async (blacklist) => {
if (!blacklist.expires || !client.user) return;
if (!blacklist.expires) return;

if (blacklist.expires < new Date()) {
await db.blacklistedServers.delete({ where: { id: blacklist.id } });
Expand All @@ -38,7 +39,6 @@ export default async function startTimers(client: Client) {
}

scheduleJob(`blacklist_server-${blacklist.serverId}`, blacklist.expires, async function() {
if (!client.user) return;
await db.blacklistedServers.delete({ where: { id: blacklist.id } });

modActions(client.user, {
Expand All @@ -52,7 +52,7 @@ export default async function startTimers(client: Client) {

// timer to unblacklist users
blacklistedUsers.forEach(async (blacklist) => {
if (!blacklist.expires || !client.user) return;
if (!blacklist.expires) return;

// if the blacklist has already expired, delete it from the database
if (blacklist.expires < new Date()) {
Expand Down

0 comments on commit 8b95980

Please sign in to comment.