import { Grammy, GrammyStatelessQ } from "../deps.ts"; import { formatUserChat } from "../utils.ts"; import { jobStore } from "../db/jobStore.ts"; import { parsePngInfo } from "../sd.ts"; import { Context, logger } from "./mod.ts"; export const txt2imgQuestion = new GrammyStatelessQ.StatelessQuestion( "txt2img", async (ctx) => { if (!ctx.message.text) return; await txt2img(ctx as any, ctx.message.text, false); }, ); export async function txt2imgCommand(ctx: Grammy.CommandContext) { await txt2img(ctx, ctx.match, true); } async function txt2img(ctx: Context, match: string, includeRepliedTo: boolean): Promise { if (!ctx.message?.from?.id) { return void ctx.reply("I don't know who you are"); } const config = ctx.session.global; if (config.pausedReason != null) { return void ctx.reply(`I'm paused: ${config.pausedReason || "No reason given"}`); } const jobs = await jobStore.getBy("status.type", "waiting"); if (jobs.length >= config.maxJobs) { return void ctx.reply( `The queue is full. Try again later. (Max queue size: ${config.maxJobs})`, ); } const userJobs = jobs.filter((job) => job.value.request.from.id === ctx.message?.from?.id); if (userJobs.length >= config.maxUserJobs) { return void ctx.reply( `You already have ${config.maxUserJobs} jobs in queue. Try again later.`, ); } let params = parsePngInfo(match); const repliedToMsg = ctx.message.reply_to_message; const repliedToText = repliedToMsg?.text || repliedToMsg?.caption; if (includeRepliedTo && repliedToText) { const originalParams = parsePngInfo(repliedToText); params = { ...originalParams, ...params, prompt: [originalParams.prompt, params.prompt].filter(Boolean).join("\n"), }; } if (!params.prompt) { return void ctx.reply( "Please tell me what you want to see." + txt2imgQuestion.messageSuffixMarkdown(), { reply_markup: { force_reply: true, selective: true }, parse_mode: "Markdown" }, ); } const replyMessage = await ctx.reply("Accepted. You are now in queue."); await jobStore.create({ params, request: ctx.message, reply: replyMessage, status: { type: "waiting" }, }); logger().debug(`Job enqueued for ${formatUserChat(ctx.message)}`); }