forked from pinks/eris
1
0
Fork 0
eris/bot/pnginfoCommand.ts

67 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-09-24 13:08:35 +00:00
import { CommandContext } from "grammy";
import { bold, fmt } from "grammy_parse_mode";
import { StatelessQuestion } from "grammy_stateless_question";
2023-10-19 21:37:03 +00:00
import { omitUndef } from "../utils/omitUndef.ts";
2023-09-24 13:08:35 +00:00
import { ErisContext } from "./mod.ts";
2023-09-26 10:43:36 +00:00
import { getPngInfo, parsePngInfo } from "./parsePngInfo.ts";
2023-09-11 20:43:12 +00:00
2023-09-24 13:08:35 +00:00
export const pnginfoQuestion = new StatelessQuestion<ErisContext>(
2023-09-11 20:43:12 +00:00
"pnginfo",
async (ctx) => {
await pnginfo(ctx, false);
},
);
2023-09-24 13:08:35 +00:00
export async function pnginfoCommand(ctx: CommandContext<ErisContext>) {
2023-09-11 20:43:12 +00:00
await pnginfo(ctx, true);
}
2023-09-24 13:08:35 +00:00
async function pnginfo(ctx: ErisContext, includeRepliedTo: boolean): Promise<void> {
2023-09-11 20:43:12 +00:00
const document = ctx.message?.document ||
(includeRepliedTo ? ctx.message?.reply_to_message?.document : undefined);
if (document?.mime_type !== "image/png" && document?.mime_type !== "image/jpeg") {
2023-09-11 20:43:12 +00:00
await ctx.reply(
2023-11-12 02:33:35 +00:00
"Please send me a PNG or JPEG file." +
2023-09-11 20:43:12 +00:00
pnginfoQuestion.messageSuffixMarkdown(),
2023-10-19 21:37:03 +00:00
omitUndef(
{
reply_markup: { force_reply: true, selective: true },
parse_mode: "Markdown",
reply_to_message_id: ctx.message?.message_id,
} as const,
),
2023-09-11 20:43:12 +00:00
);
return;
}
const file = await ctx.api.getFile(document.file_id);
const buffer = await fetch(file.getUrl()).then((resp) => resp.arrayBuffer());
2023-11-12 02:33:35 +00:00
const info = getPngInfo(buffer);
if (!info) {
2023-11-17 18:22:35 +00:00
return void await ctx.reply(
2023-11-12 02:33:35 +00:00
"No info found in file.",
omitUndef({ reply_to_message_id: ctx.message?.message_id }),
);
}
const params = parsePngInfo(info, undefined, true);
2023-09-11 20:43:12 +00:00
const paramsText = fmt([
`${params.prompt}\n`,
params.negative_prompt ? fmt`${bold("Negative prompt:")} ${params.negative_prompt}\n` : "",
params.steps ? fmt`${bold("Steps:")} ${params.steps}, ` : "",
params.sampler_name ? fmt`${bold("Sampler:")} ${params.sampler_name}, ` : "",
params.cfg_scale ? fmt`${bold("CFG scale:")} ${params.cfg_scale}, ` : "",
params.seed ? fmt`${bold("Seed:")} ${params.seed}, ` : "",
params.width && params.height ? fmt`${bold("Size")}: ${params.width}x${params.height}` : "",
]);
2023-10-19 21:37:03 +00:00
await ctx.reply(
paramsText.text,
omitUndef({
entities: paramsText.entities,
reply_to_message_id: ctx.message?.message_id,
}),
);
2023-09-11 20:43:12 +00:00
}