(tasks): added basic scheduled tasks system.

This commit is contained in:
2026-08-28 01:00:44 +08:00
parent f353a73a9e
commit 42b67ef4da
6 changed files with 67 additions and 0 deletions
+6
View File
@@ -1,10 +1,16 @@
import { Event, CommandClient } from 'athena-prime';
import tasks from '../../tasks';
class ReadyEvent extends Event<CommandClient> {
event: string = 'ready' as const;
async handle(context: CommandClient<any, any>) {
await context.deployCommands();
tasks.forEach(async task => {
if (task.init) await task.handler(context);
setInterval(async () => await task.handler(context), task.interval);
});
}
}
+1
View File
@@ -1,5 +1,6 @@
import { CommandClient, Constants } from "athena-prime";
import events from "./events";
import tasks from "./tasks";
// ----------
+21
View File
@@ -0,0 +1,21 @@
import { CommandClient } from "athena-prime";
import { Task } from "../types/tasks";
import { database } from '../utils';
class DeleteExpiredConfigsTask implements Task {
interval: number = 360;
init: boolean = true;
async handler(context: CommandClient): Promise<void> {
const configs = await database.getConfigurations();
for (const config of configs) {
const guild = context.guilds.get(config.id);
if (guild) continue;
await database.deleteConfiguration(config.id);
}
}
}
export default new DeleteExpiredConfigsTask();
+5
View File
@@ -0,0 +1,5 @@
import deleteExpiredConfigs from "./deleteExpiredConfigs";
export default [
deleteExpiredConfigs
];
+23
View File
@@ -0,0 +1,23 @@
import { CommandClient } from "athena-prime";
// ----------
/**
* A scheduled task.
*/
export type Task = {
/**
* The interval before running the task.
*/
interval: number;
/**
* Runs the task when it's loaded.
*/
init: boolean;
/**
* The task handler.
*/
handler: (context: CommandClient) => Promise<void>;
}
+11
View File
@@ -62,6 +62,17 @@ export async function getConfiguration(guildId: string): Promise<Configuration>
return (row?.config ?? {}) as Configuration;
}
export async function getConfigurations(): Promise<{ id: string; config: Configuration }[]> {
const rows = await client`
SELECT guild_id, config FROM settings;
`;
return rows.map((row) => ({
id: row.guild_id,
config: row.config as Configuration,
}));
}
export async function createConfiguration(guildId: string): Promise<void> {
await client`
INSERT INTO settings (guild_id, config)