reupload attachments for chat interactions

This commit is contained in:
artie 2025-02-16 02:17:02 +01:00
parent 372746eba9
commit bb5421a2a9
3 changed files with 28 additions and 17 deletions

View File

@ -1,6 +1,6 @@
import { import {
AttachmentBuilder,
AutocompleteInteraction, AutocompleteInteraction,
hyperlink,
inlineCode, inlineCode,
SlashCommandBuilder, SlashCommandBuilder,
type InteractionReplyOptions, type InteractionReplyOptions,
@ -43,7 +43,7 @@ export async function translateImpl(
source: string | null, source: string | null,
target: string, target: string,
ocrModel?: OCRResult["model"], ocrModel?: OCRResult["model"],
imageUrl?: string image?: AttachmentBuilder
) { ) {
let { translatedText, detectedSourceLang, model } = await translateDeepl( let { translatedText, detectedSourceLang, model } = await translateDeepl(
text, text,
@ -65,7 +65,7 @@ export async function translateImpl(
return { return {
content: ocrModel content: ocrModel
? `OCR: ${inlineCode(capitalize(ocrModel))}` ? `OCR: ${inlineCode(capitalize(ocrModel))}`
: "" + (imageUrl ? `\n${hyperlink("Image", imageUrl)}` : ""), : undefined,
files: [ files: [
{ {
name: `${displaySource}-${displayTarget}.txt`, name: `${displaySource}-${displayTarget}.txt`,
@ -73,17 +73,18 @@ export async function translateImpl(
`--- From ${displaySource} to ${displayTarget} ---\n${translatedText}` `--- From ${displaySource} to ${displayTarget} ---\n${translatedText}`
), ),
}, },
...(image ? [image] : []),
], ],
} satisfies InteractionReplyOptions; } satisfies InteractionReplyOptions;
} }
return { return {
...(image ? { files: [image] } : {}),
embeds: [ embeds: [
{ {
title: `From ${displaySource} to ${displayTarget}`, title: `From ${displaySource} to ${displayTarget}`,
description: translatedText, description: translatedText,
color: model === "deepl" ? 0x0f2b46 : 0x4285f4, color: model === "deepl" ? 0x0f2b46 : 0x4285f4,
...(imageUrl ? { image: { url: imageUrl } } : {}),
author: { author: {
name: model === "deepl" ? "DeepL" : "Google Translate", name: model === "deepl" ? "DeepL" : "Google Translate",
icon_url: `https://www.google.com/s2/favicons?domain=${model}.com&sz=64`, icon_url: `https://www.google.com/s2/favicons?domain=${model}.com&sz=64`,
@ -96,6 +97,7 @@ export async function translateImpl(
}, },
} }
: {}), : {}),
...(image ? { image: { url: "attachment://image.jpg" } } : {}),
}, },
], ],
} satisfies InteractionReplyOptions; } satisfies InteractionReplyOptions;

View File

@ -1,5 +1,5 @@
import { import {
hyperlink, AttachmentBuilder,
inlineCode, inlineCode,
SlashCommandBuilder, SlashCommandBuilder,
type InteractionReplyOptions, type InteractionReplyOptions,
@ -22,7 +22,7 @@ export function buildOcrPayload(
text: string, text: string,
language: string, language: string,
model: OCRResult["model"], model: OCRResult["model"],
imageUrl?: string image?: AttachmentBuilder
) { ) {
const languageName = languageCodeToName(language) ?? "Unknown"; const languageName = languageCodeToName(language) ?? "Unknown";
@ -30,18 +30,19 @@ export function buildOcrPayload(
return { return {
content: content:
`Detected language: ${inlineCode(languageName)}` + `Detected language: ${inlineCode(languageName)}` +
`\nOCR: ${inlineCode(capitalize(model))}` + `\nOCR: ${inlineCode(capitalize(model))}`,
(imageUrl ? `\n${hyperlink("Image", imageUrl)}` : ""),
files: [ files: [
{ {
name: "ocr.txt", name: "ocr.txt",
attachment: Buffer.from(text), attachment: Buffer.from(text),
}, },
...(image ? [image] : []),
], ],
} satisfies InteractionReplyOptions; } satisfies InteractionReplyOptions;
} }
return { return {
...(image ? { files: [image] } : {}),
embeds: [ embeds: [
{ {
description: text, description: text,
@ -55,11 +56,11 @@ export function buildOcrPayload(
}, },
] ]
: [], : [],
...(imageUrl ? { image: { url: imageUrl } } : {}),
author: { author: {
name: capitalize(model), name: capitalize(model),
icon_url: `https://www.google.com/s2/favicons?domain=${model}.com&sz=64`, icon_url: `https://www.google.com/s2/favicons?domain=${model}.com&sz=64`,
}, },
...(image ? { image: { url: "attachment://image.jpg" } } : {}),
}, },
], ],
} satisfies InteractionReplyOptions; } satisfies InteractionReplyOptions;
@ -75,13 +76,12 @@ export async function ocrImpl(url: string) {
}); });
if (!type?.mime.startsWith("image/")) { if (!type?.mime.startsWith("image/")) {
console.log(type, url);
abort("Not a valid image!"); abort("Not a valid image!");
} }
const compressed = await sharp(data) const compressed = await sharp(data)
.resize(1000) .resize({ width: 1000, withoutEnlargement: true })
.jpeg({ quality: 90 }) .jpeg({ quality: 95 })
.toBuffer(); .toBuffer();
const result = await lensOcr(compressed) const result = await lensOcr(compressed)
@ -92,7 +92,10 @@ export async function ocrImpl(url: string) {
result.text = "No text detected"; result.text = "No text detected";
} }
return result; return {
...result,
attachment: new AttachmentBuilder(compressed).setName("image.jpg"),
};
} }
export default defineCommand({ export default defineCommand({
@ -111,8 +114,8 @@ export default defineCommand({
await interaction.deferReply(); await interaction.deferReply();
const { text, language, model } = await ocrImpl(imageUrl); const { text, language, model, attachment } = await ocrImpl(imageUrl);
const payload = buildOcrPayload(text, language, model, imageUrl); const payload = buildOcrPayload(text, language, model, attachment);
await interaction.editReply(payload); await interaction.editReply(payload);
}, },
}); });

View File

@ -51,8 +51,14 @@ export default defineCommand({
abort("Target language not found"); abort("Target language not found");
} }
const { text, model } = await ocrImpl(imageUrl); const { text, model, attachment } = await ocrImpl(imageUrl);
const payload = await translateImpl(text, source, target, model, imageUrl); const payload = await translateImpl(
text,
source,
target,
model,
attachment
);
await interaction.editReply(payload); await interaction.editReply(payload);
}, },
}); });