Files
Civita/dist/scheduler/qotd.js
2025-10-20 16:30:50 +02:00

40 lines
1.5 KiB
JavaScript

import { withConn } from '../db/pool.js';
import { generateQOTD } from '../ai/gemini.js';
export function startQotdScheduler(client) {
const run = async () => {
const today = new Date();
const yyyyMmDd = today.toISOString().slice(0, 10);
const guilds = await withConn(async (conn) => {
return conn.query('SELECT guild_id, qotd_channel_id, qotd_last_sent FROM guild_config WHERE qotd_enabled = 1 AND qotd_channel_id IS NOT NULL');
});
for (const row of guilds) {
if (row.qotd_last_sent && row.qotd_last_sent.toISOString().slice(0, 10) === yyyyMmDd)
continue;
const channel = client.channels.cache.get(row.qotd_channel_id);
if (!channel)
continue;
try {
const question = await generateQOTD();
await channel.send(`QOTD: ${question}`);
await withConn((conn) => conn.query('UPDATE guild_config SET qotd_last_sent = ? WHERE guild_id = ?', [yyyyMmDd, row.guild_id]));
}
catch (err) {
// ignore individual failures
}
}
};
// Run daily at 09:00 UTC
const scheduleNext = () => {
const now = new Date();
const next = new Date();
next.setUTCHours(9, 0, 0, 0);
if (next <= now)
next.setUTCDate(next.getUTCDate() + 1);
setTimeout(async () => {
await run();
scheduleNext();
}, next.getTime() - now.getTime());
};
scheduleNext();
}