diff --git a/package.json b/package.json index c5af404..27b06a3 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "@discordjs/rest": "^2.0.0", "@types/jest": "^29.0.0", "@types/uuid": "^9.0.0", + "cron": "^3.3.1", "discord.js": "^14.3.0", "dotenv": "^16.0.0", "emoji-regex": "^10.0.0", diff --git a/src/client/client.ts b/src/client/client.ts index c1ce5da..b51ccda 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -1,6 +1,5 @@ import { Client, Partials } from "discord.js"; import * as dotenv from "dotenv"; -import { createConnection } from "typeorm"; import { EventType } from "../constants/EventType"; import ICommandItem from "../contracts/ICommandItem"; import IEventItem from "../contracts/IEventItem"; @@ -12,6 +11,7 @@ import AppDataSource from "../database/dataSources/appDataSource"; import ButtonEventItem from "../contracts/ButtonEventItem"; import { ButtonEvent } from "../type/buttonEvent"; import CacheHelper from "../helpers/CacheHelper"; +import TimerHelper from "../helpers/TimerHelper"; export class CoreClient extends Client { private static _commandItems: ICommandItem[]; @@ -20,6 +20,7 @@ export class CoreClient extends Client { private _events: Events; private _util: Util; + private _timerHelper: TimerHelper; public static get commandItems(): ICommandItem[] { return this._commandItems; @@ -43,6 +44,7 @@ export class CoreClient extends Client { this._events = new Events(); this._util = new Util(); + this._timerHelper = new TimerHelper(); } public async start() { @@ -52,7 +54,11 @@ export class CoreClient extends Client { } await AppDataSource.initialize() - .then(() => console.log("Data Source Initialized")) + .then(() => { + console.log("Data Source Initialized"); + + // this.timerHelper.AddTimer + }) .catch((err) => console.error("Error Initialising Data Source", err)); super.on("interactionCreate", this._events.onInteractionCreate); diff --git a/src/helpers/TimerHelper.ts b/src/helpers/TimerHelper.ts new file mode 100644 index 0000000..3cc4246 --- /dev/null +++ b/src/helpers/TimerHelper.ts @@ -0,0 +1,81 @@ +import {CronJob} from "cron"; +import {primitive} from "../type/primitive"; +import {v4} from "uuid"; + +interface Timer { + id: string; + job: CronJob; + context: Map; + onTick: ((context: Map) => void) | ((context: Map) => Promise); + runOnStart: boolean; +} + +export default class TimerHelper { + private _timers: Timer[]; + + constructor() { + this._timers = []; + } + + public AddTimer( + cronTime: string, + timeZone: string, + onTick: ((context: Map) => void) | ((context: Map) => Promise), + runOnStart: boolean = false): string { + const context = new Map(); + + const job = new CronJob( + cronTime, + () => { + onTick(context); + }, + null, + false, + timeZone, + ); + + const id = v4(); + + this._timers.push({ + id, + job, + context, + onTick, + runOnStart, + }); + + return id; + } + + public StartAllTimers() { + this._timers.forEach(timer => this.StartJob(timer)); + } + + public StopAllTimers() { + this._timers.forEach(timer => timer.job.stop()); + } + + public StartTimer(id: string) { + const timer = this._timers.find(x => x.id == id); + + if (!timer) return; + + this.StartJob(timer); + } + + public StopTimer(id: string) { + const timer = this._timers.find(x => x.id == id); + + if (!timer) return; + + timer.job.stop(); + } + + private StartJob(timer: Timer) { + timer.job.start(); + + if (timer.runOnStart) { + timer.onTick(timer.context); + } + } +} diff --git a/src/type/primitive.ts b/src/type/primitive.ts new file mode 100644 index 0000000..9093737 --- /dev/null +++ b/src/type/primitive.ts @@ -0,0 +1 @@ +export type primitive = string | number | boolean; diff --git a/yarn.lock b/yarn.lock index ced241e..24feeb5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -788,6 +788,11 @@ expect "^29.0.0" pretty-format "^29.0.0" +"@types/luxon@~3.4.0": + version "3.4.2" + resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-3.4.2.tgz#e4fc7214a420173cea47739c33cdf10874694db7" + integrity sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA== + "@types/node@*": version "22.7.5" resolved "https://registry.npmjs.org/@types/node/-/node-22.7.5.tgz" @@ -1458,6 +1463,14 @@ create-jest@^29.7.0: jest-util "^29.7.0" prompts "^2.0.1" +cron@^3.3.1: + version "3.3.1" + resolved "https://registry.yarnpkg.com/cron/-/cron-3.3.1.tgz#03c56b4a3ad52606160adfba1fab932c53838807" + integrity sha512-KpvuzJEbeTMTfLsXhUuDfsFYr8s5roUlLKb4fa68GszWrA4783C7q6m9yj4vyc6neyD/V9e0YiADSX2c+yRDXg== + dependencies: + "@types/luxon" "~3.4.0" + luxon "~3.5.0" + cross-spawn@^7.0.0, cross-spawn@^7.0.3: version "7.0.3" resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz" @@ -3076,6 +3089,11 @@ lru-cache@^5.1.1: dependencies: yallist "^3.0.2" +luxon@~3.5.0: + version "3.5.0" + resolved "https://registry.yarnpkg.com/luxon/-/luxon-3.5.0.tgz#6b6f65c5cd1d61d1fd19dbf07ee87a50bf4b8e20" + integrity sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ== + magic-bytes.js@^1.10.0: version "1.10.0" resolved "https://registry.npmjs.org/magic-bytes.js/-/magic-bytes.js-1.10.0.tgz"