new scheduler? in 2026? nahhh
This commit is contained in:
+5
-9
@@ -4,8 +4,9 @@ import { handleAuditLogCreate, handleBanRemove, handleBulkMessageDelete, handleG
|
|||||||
import { Database } from './shared/Database';
|
import { Database } from './shared/Database';
|
||||||
import { openRedisClient } from './shared/RedisClient';
|
import { openRedisClient } from './shared/RedisClient';
|
||||||
import { Handler } from './types';
|
import { Handler } from './types';
|
||||||
import { checkExpiredBans, closeOldTickets, initIfNecessary, loadHandlersFrom, refreshCommands } from './utils';
|
import { initIfNecessary, loadHandlersFrom, refreshCommands } from './utils';
|
||||||
import { initializeWebserver } from './webserver';
|
import { initializeWebserver } from './webserver';
|
||||||
|
import { Scheduler, ScheduledTasks } from './scheduler';
|
||||||
|
|
||||||
let ready = false;
|
let ready = false;
|
||||||
|
|
||||||
@@ -28,6 +29,8 @@ const client = new DiscordClient({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const scheduler: Scheduler = new Scheduler(client);
|
||||||
|
|
||||||
const commands: Handler[] = [];
|
const commands: Handler[] = [];
|
||||||
const contextMenus: Handler[] = [];
|
const contextMenus: Handler[] = [];
|
||||||
const buttons: Handler[] = [];
|
const buttons: Handler[] = [];
|
||||||
@@ -135,16 +138,9 @@ client.on('clientReady', async () => {
|
|||||||
|
|
||||||
await initializeWebserver(client);
|
await initializeWebserver(client);
|
||||||
|
|
||||||
// Check for expired bans every 5 minutes
|
ScheduledTasks.forEach((task) => scheduler.add(task));
|
||||||
checkExpiredBans(client);
|
|
||||||
setInterval(checkExpiredBans.bind(null, client), 300000);
|
|
||||||
|
|
||||||
// Close old tickets every hour
|
|
||||||
closeOldTickets(client);
|
|
||||||
setInterval(closeOldTickets.bind(null, client), 3.6e+6);
|
|
||||||
|
|
||||||
ready = true;
|
ready = true;
|
||||||
|
|
||||||
console.log('Ready');
|
console.log('Ready');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { Client } from "discord.js";
|
||||||
|
import ScheduledTasks from "./tasks";
|
||||||
|
import { Task } from "../types";
|
||||||
|
|
||||||
|
class Scheduler {
|
||||||
|
private tasks = new Map<Task, NodeJS.Timeout>();
|
||||||
|
private context: Client;
|
||||||
|
|
||||||
|
constructor(context: Client) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
add(task: Task) {
|
||||||
|
const interval = setInterval(() => task.handle(this.context), task.interval);
|
||||||
|
if (task.firstRun) interval._onTimeout();
|
||||||
|
|
||||||
|
this.tasks.set(task, interval);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
Scheduler,
|
||||||
|
ScheduledTasks
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { Client } from "discord.js";
|
||||||
|
import { Task } from "../../types";
|
||||||
|
import { checkExpiredBans } from "../../utils";
|
||||||
|
|
||||||
|
const checkExpiredBansTask: Task = {
|
||||||
|
interval: 300000,
|
||||||
|
firstRun: true,
|
||||||
|
|
||||||
|
handle: async (context: Client) => await checkExpiredBans(context)
|
||||||
|
};
|
||||||
|
|
||||||
|
export default checkExpiredBansTask;
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
import { Client } from "discord.js";
|
||||||
|
import { Task } from "../../types";
|
||||||
|
import { closeOldTickets } from "../../utils";
|
||||||
|
|
||||||
|
const closeStaleTicketsTask: Task = {
|
||||||
|
interval: 3.6e+6,
|
||||||
|
firstRun: true,
|
||||||
|
|
||||||
|
handle: async (context: Client) => await closeOldTickets(context)
|
||||||
|
};
|
||||||
|
|
||||||
|
export default closeStaleTicketsTask;
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import checkExpiredBansTask from './check-expired-bans-task';
|
||||||
|
import closeStaleTicketsTask from './close-stale-tickets-task';
|
||||||
|
|
||||||
|
export default [
|
||||||
|
closeStaleTicketsTask,
|
||||||
|
checkExpiredBansTask,
|
||||||
|
];
|
||||||
Vendored
+1
@@ -3,3 +3,4 @@ export * from './database-types.d';
|
|||||||
export * from './e621-types.d';
|
export * from './e621-types.d';
|
||||||
export * from './handler.d';
|
export * from './handler.d';
|
||||||
export * from './helper-types.d';
|
export * from './helper-types.d';
|
||||||
|
export * from './scheduler.d';
|
||||||
Vendored
+8
@@ -0,0 +1,8 @@
|
|||||||
|
import { Client } from "discord.js";
|
||||||
|
|
||||||
|
export type Task = {
|
||||||
|
interval: number;
|
||||||
|
firstRun: boolean;
|
||||||
|
|
||||||
|
handle: (context: Client) => Promise<void>;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user