forked from pinks/eris
1
0
Fork 0
nyx/bot/queueCommand.ts

68 lines
2.7 KiB
TypeScript
Raw Normal View History

2023-09-10 18:56:17 +00:00
import { Grammy, GrammyParseMode } from "../deps.ts";
2023-09-13 09:50:22 +00:00
import { fmt } from "../common/utils.ts";
2023-09-10 18:56:17 +00:00
import { runningWorkers } from "../tasks/pingWorkers.ts";
import { jobStore } from "../db/jobStore.ts";
import { Context, logger } from "./mod.ts";
2023-09-13 09:50:22 +00:00
import { getFlagEmoji } from "../common/getFlagEmoji.ts";
2023-09-10 18:56:17 +00:00
export async function queueCommand(ctx: Grammy.CommandContext<Context>) {
let formattedMessage = await getMessageText();
const queueMessage = await ctx.replyFmt(formattedMessage);
handleFutureUpdates().catch((err) => logger().warning(`Updating queue message failed: ${err}`));
async function getMessageText() {
const processingJobs = await jobStore.getBy("status.type", "processing")
.then((jobs) => jobs.map((job) => ({ ...job.value, place: 0 })));
const waitingJobs = await jobStore.getBy("status.type", "waiting")
.then((jobs) => jobs.map((job, index) => ({ ...job.value, place: index + 1 })));
const jobs = [...processingJobs, ...waitingJobs];
const { bold } = GrammyParseMode;
2023-09-10 18:56:17 +00:00
return fmt([
"Current queue:\n",
...jobs.length > 0
? jobs.flatMap((job) => [
`${job.place}. `,
2023-09-12 01:57:44 +00:00
fmt`${bold(job.from.first_name)} `,
job.from.last_name ? fmt`${bold(job.from.last_name)} ` : "",
job.from.username ? `(@${job.from.username}) ` : "",
getFlagEmoji(job.from.language_code) ?? "",
job.chat.type === "private" ? " in private chat " : ` in ${job.chat.title} `,
job.chat.type !== "private" && job.chat.type !== "group" &&
job.chat.username
? `(@${job.chat.username}) `
2023-09-10 18:56:17 +00:00
: "",
job.status.type === "processing"
? `(${(job.status.progress * 100).toFixed(0)}% using ${job.status.worker}) `
: "",
"\n",
])
: ["Queue is empty.\n"],
"\nActive workers:\n",
2023-09-11 17:08:12 +00:00
...ctx.session.global.workers.flatMap((worker) => [
2023-09-12 22:53:50 +00:00
runningWorkers.has(worker.id) ? "✅ " : "☠️ ",
fmt`${bold(worker.name || worker.id)} `,
2023-09-10 18:56:17 +00:00
`(max ${(worker.maxResolution / 1000000).toFixed(1)} Mpx) `,
"\n",
]),
]);
}
async function handleFutureUpdates() {
for (let idx = 0; idx < 30; idx++) {
2023-09-13 00:38:09 +00:00
await ctx.api.sendChatAction(ctx.chat.id, "typing", { maxAttempts: 1 } as never);
await new Promise((resolve) => setTimeout(resolve, 4000));
2023-09-10 18:56:17 +00:00
const nextFormattedMessage = await getMessageText();
if (nextFormattedMessage.text !== formattedMessage.text) {
await ctx.api.editMessageText(
ctx.chat.id,
queueMessage.message_id,
nextFormattedMessage.text,
{ entities: nextFormattedMessage.entities },
);
formattedMessage = nextFormattedMessage;
}
}
}
}