import { Events } from "discord.js"; import { Event } from "@voidy/framework"; import { ChatbotChannel } from "../schemas/ChatbotChannel"; import { enqueueNotification } from "../../../queue"; const TICK_MS = 5_000; // check every 5 seconds export default new Event({ id: "chatbotScheduler", name: Events.ClientReady, once: true, execute: async () => { console.log("[Chatbot] Scheduler started"); const tick = async () => { try { const now = new Date(); const channels = await ChatbotChannel.find({ enabled: true }); for (const ch of channels) { const elapsed = ch.lastSentAt ? (now.getTime() - ch.lastSentAt.getTime()) / 1000 : Infinity; if (elapsed >= ch.intervalSeconds) { await enqueueNotification("chatbot", { channelId: ch.channelId, guildId: ch.guildId, }); } } } catch (err) { console.error("[Chatbot] Scheduler error:", err); } }; setInterval(tick, TICK_MS); }, });