diff --git a/src/commands/language/translate.ts b/src/commands/language/translate.ts index 0816f16..74144f5 100644 --- a/src/commands/language/translate.ts +++ b/src/commands/language/translate.ts @@ -1,6 +1,6 @@ import { + AttachmentBuilder, AutocompleteInteraction, - hyperlink, inlineCode, SlashCommandBuilder, type InteractionReplyOptions, @@ -43,7 +43,7 @@ export async function translateImpl( source: string | null, target: string, ocrModel?: OCRResult["model"], - imageUrl?: string + image?: AttachmentBuilder ) { let { translatedText, detectedSourceLang, model } = await translateDeepl( text, @@ -65,7 +65,7 @@ export async function translateImpl( return { content: ocrModel ? `OCR: ${inlineCode(capitalize(ocrModel))}` - : "" + (imageUrl ? `\n${hyperlink("Image", imageUrl)}` : ""), + : undefined, files: [ { name: `${displaySource}-${displayTarget}.txt`, @@ -73,17 +73,18 @@ export async function translateImpl( `--- From ${displaySource} to ${displayTarget} ---\n${translatedText}` ), }, + ...(image ? [image] : []), ], } satisfies InteractionReplyOptions; } return { + ...(image ? { files: [image] } : {}), embeds: [ { title: `From ${displaySource} to ${displayTarget}`, description: translatedText, color: model === "deepl" ? 0x0f2b46 : 0x4285f4, - ...(imageUrl ? { image: { url: imageUrl } } : {}), author: { name: model === "deepl" ? "DeepL" : "Google Translate", 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; diff --git a/src/commands/ocr/ocr.ts b/src/commands/ocr/ocr.ts index 51eaab1..8110f24 100644 --- a/src/commands/ocr/ocr.ts +++ b/src/commands/ocr/ocr.ts @@ -1,5 +1,5 @@ import { - hyperlink, + AttachmentBuilder, inlineCode, SlashCommandBuilder, type InteractionReplyOptions, @@ -22,7 +22,7 @@ export function buildOcrPayload( text: string, language: string, model: OCRResult["model"], - imageUrl?: string + image?: AttachmentBuilder ) { const languageName = languageCodeToName(language) ?? "Unknown"; @@ -30,18 +30,19 @@ export function buildOcrPayload( return { content: `Detected language: ${inlineCode(languageName)}` + - `\nOCR: ${inlineCode(capitalize(model))}` + - (imageUrl ? `\n${hyperlink("Image", imageUrl)}` : ""), + `\nOCR: ${inlineCode(capitalize(model))}`, files: [ { name: "ocr.txt", attachment: Buffer.from(text), }, + ...(image ? [image] : []), ], } satisfies InteractionReplyOptions; } return { + ...(image ? { files: [image] } : {}), embeds: [ { description: text, @@ -55,11 +56,11 @@ export function buildOcrPayload( }, ] : [], - ...(imageUrl ? { image: { url: imageUrl } } : {}), author: { name: capitalize(model), icon_url: `https://www.google.com/s2/favicons?domain=${model}.com&sz=64`, }, + ...(image ? { image: { url: "attachment://image.jpg" } } : {}), }, ], } satisfies InteractionReplyOptions; @@ -75,13 +76,12 @@ export async function ocrImpl(url: string) { }); if (!type?.mime.startsWith("image/")) { - console.log(type, url); abort("Not a valid image!"); } const compressed = await sharp(data) - .resize(1000) - .jpeg({ quality: 90 }) + .resize({ width: 1000, withoutEnlargement: true }) + .jpeg({ quality: 95 }) .toBuffer(); const result = await lensOcr(compressed) @@ -92,7 +92,10 @@ export async function ocrImpl(url: string) { result.text = "No text detected"; } - return result; + return { + ...result, + attachment: new AttachmentBuilder(compressed).setName("image.jpg"), + }; } export default defineCommand({ @@ -111,8 +114,8 @@ export default defineCommand({ await interaction.deferReply(); - const { text, language, model } = await ocrImpl(imageUrl); - const payload = buildOcrPayload(text, language, model, imageUrl); + const { text, language, model, attachment } = await ocrImpl(imageUrl); + const payload = buildOcrPayload(text, language, model, attachment); await interaction.editReply(payload); }, }); diff --git a/src/commands/ocr/ocrTranslate.ts b/src/commands/ocr/ocrTranslate.ts index a9c4aac..7a0aa6e 100644 --- a/src/commands/ocr/ocrTranslate.ts +++ b/src/commands/ocr/ocrTranslate.ts @@ -51,8 +51,14 @@ export default defineCommand({ abort("Target language not found"); } - const { text, model } = await ocrImpl(imageUrl); - const payload = await translateImpl(text, source, target, model, imageUrl); + const { text, model, attachment } = await ocrImpl(imageUrl); + const payload = await translateImpl( + text, + source, + target, + model, + attachment + ); await interaction.editReply(payload); }, });