Initial commit
This commit is contained in:
39
dist/scheduler/qotd.js
vendored
Normal file
39
dist/scheduler/qotd.js
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user