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

73 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2023-09-24 13:08:35 +00:00
import { CommandContext } from "grammy";
import { bold, fmt } from "grammy_parse_mode";
2023-09-23 18:49:05 +00:00
import { getConfig } from "../app/config.ts";
2023-09-24 13:08:35 +00:00
import { activeGenerationWorkers, generationQueue } from "../app/generationQueue.ts";
import { getFlagEmoji } from "../utils/getFlagEmoji.ts";
import { ErisContext } from "./mod.ts";
2023-09-10 18:56:17 +00:00
2023-09-24 13:08:35 +00:00
export async function queueCommand(ctx: CommandContext<ErisContext>) {
2023-09-10 18:56:17 +00:00
let formattedMessage = await getMessageText();
2023-09-24 19:58:09 +00:00
const queueMessage = await ctx.replyFmt(formattedMessage, {
disable_notification: true,
reply_to_message_id: ctx.message?.message_id,
});
handleFutureUpdates().catch(() => undefined);
2023-09-10 18:56:17 +00:00
async function getMessageText() {
2023-09-22 02:59:22 +00:00
const config = await getConfig();
const allJobs = await generationQueue.getAllJobs();
const processingJobs = allJobs
.filter((job) => job.lockUntil > new Date()).map((job) => ({ ...job, index: 0 }));
const waitingJobs = allJobs
.filter((job) => job.lockUntil <= new Date())
.map((job, index) => ({ ...job, index: index + 1 }));
2023-09-10 18:56:17 +00:00
const jobs = [...processingJobs, ...waitingJobs];
2023-09-10 18:56:17 +00:00
return fmt([
"Current queue:\n",
...jobs.length > 0
? jobs.flatMap((job) => [
2023-09-22 02:59:22 +00:00
`${job.index}. `,
fmt`${bold(job.state.from.first_name)} `,
job.state.from.last_name ? fmt`${bold(job.state.from.last_name)} ` : "",
job.state.from.username ? `(@${job.state.from.username}) ` : "",
getFlagEmoji(job.state.from.language_code) ?? "",
job.state.chat.type === "private" ? " in private chat " : ` in ${job.state.chat.title} `,
job.state.chat.type !== "private" && job.state.chat.type !== "group" &&
job.state.chat.username
? `(@${job.state.chat.username}) `
2023-09-10 18:56:17 +00:00
: "",
2023-09-22 02:59:22 +00:00
job.index === 0 && job.state.progress && job.state.sdInstanceId
? `(${(job.state.progress * 100).toFixed(0)}% using ${job.state.sdInstanceId}) `
2023-09-10 18:56:17 +00:00
: "",
"\n",
])
: ["Queue is empty.\n"],
"\nActive workers:\n",
2023-10-05 09:00:51 +00:00
...Object.entries(config.sdInstances).flatMap(([sdInstanceId, sdInstance]) => [
activeGenerationWorkers.get(sdInstanceId)?.isProcessing ? "✅ " : "☠️ ",
fmt`${bold(sdInstance.name || sdInstanceId)} `,
2023-09-22 02:59:22 +00:00
`(max ${(sdInstance.maxResolution / 1000000).toFixed(1)} Mpx) `,
2023-09-10 18:56:17 +00:00
"\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;
}
}
}
}