Files
illegitimate-bot/src/utils/Events/cron.ts
2025-03-01 21:18:26 +01:00

21 lines
817 B
TypeScript

import { CronJob } from "cron"
import fs from "fs"
import path from "path"
import { ICron } from "~/typings"
type FileType = "js" | "ts"
export default async function loadCronEvents(ft: FileType) {
const cronPath = path.join(import.meta.dirname, "..", "..", "events", "cron")
const cronFiles = fs.readdirSync(cronPath).filter(file => file.endsWith(ft))
for (const file of cronFiles) {
const filePath = path.join(cronPath, file)
const { default: cron } = await import("file://" + filePath) as { default: ICron }
const { seconds, minutes, hours, dayOfMonth, month, dayOfWeek } = cron.time
const time = `${seconds} ${minutes} ${hours} ${dayOfMonth} ${month} ${dayOfWeek}`
new CronJob(time, cron.execute, cron.onComplete, cron.start, cron.timeZone).start()
}
}