eris/tasks/updateJobStatusMsgs.ts

29 lines
888 B
TypeScript
Raw Normal View History

2023-09-10 18:56:17 +00:00
import { Log } from "../deps.ts";
import { bot } from "../bot/mod.ts";
import { formatOrdinal } from "../utils.ts";
import { jobStore } from "../db/jobStore.ts";
const logger = () => Log.getLogger();
/**
* Updates status messages for jobs in the queue.
*/
export async function updateJobStatusMsgs(): Promise<never> {
while (true) {
try {
await new Promise((resolve) => setTimeout(resolve, 5000));
const jobs = await jobStore.getBy("status.type", "waiting");
for (const [index, job] of jobs.entries()) {
if (!job.value.reply) continue;
await bot.api.editMessageText(
job.value.reply.chat.id,
job.value.reply.message_id,
`You are ${formatOrdinal(index + 1)} in queue.`,
).catch(() => undefined);
}
} catch (err) {
logger().warning(`Updating job status messages failed: ${err}`);
}
}
}