mirror of
https://github.com/nx-bat/synapse.git
synced 2026-09-22 13:19:06 -04:00
(tasks): added basic scheduled tasks system.
This commit is contained in:
@@ -1,10 +1,16 @@
|
|||||||
import { Event, CommandClient } from 'athena-prime';
|
import { Event, CommandClient } from 'athena-prime';
|
||||||
|
import tasks from '../../tasks';
|
||||||
|
|
||||||
class ReadyEvent extends Event<CommandClient> {
|
class ReadyEvent extends Event<CommandClient> {
|
||||||
event: string = 'ready' as const;
|
event: string = 'ready' as const;
|
||||||
|
|
||||||
async handle(context: CommandClient<any, any>) {
|
async handle(context: CommandClient<any, any>) {
|
||||||
await context.deployCommands();
|
await context.deployCommands();
|
||||||
|
|
||||||
|
tasks.forEach(async task => {
|
||||||
|
if (task.init) await task.handler(context);
|
||||||
|
setInterval(async () => await task.handler(context), task.interval);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { CommandClient, Constants } from "athena-prime";
|
import { CommandClient, Constants } from "athena-prime";
|
||||||
import events from "./events";
|
import events from "./events";
|
||||||
|
import tasks from "./tasks";
|
||||||
|
|
||||||
// ----------
|
// ----------
|
||||||
|
|
||||||
|
|||||||
@@ -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();
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
import deleteExpiredConfigs from "./deleteExpiredConfigs";
|
||||||
|
|
||||||
|
export default [
|
||||||
|
deleteExpiredConfigs
|
||||||
|
];
|
||||||
Vendored
+23
@@ -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>;
|
||||||
|
}
|
||||||
@@ -62,6 +62,17 @@ export async function getConfiguration(guildId: string): Promise<Configuration>
|
|||||||
return (row?.config ?? {}) as 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> {
|
export async function createConfiguration(guildId: string): Promise<void> {
|
||||||
await client`
|
await client`
|
||||||
INSERT INTO settings (guild_id, config)
|
INSERT INTO settings (guild_id, config)
|
||||||
|
|||||||
Reference in New Issue
Block a user