From 35a95f651eefe6f253a62cd33b47eba6fefd842f Mon Sep 17 00:00:00 2001 From: artie Date: Tue, 15 Apr 2025 19:00:08 +0200 Subject: [PATCH] support forwarded messages in ocr menus, disable glens fallback error reporting --- src/commands/ocr/ocr.ts | 4 ++-- src/utils/functions.ts | 30 ++++++++++++++++++++++-------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/commands/ocr/ocr.ts b/src/commands/ocr/ocr.ts index 0972be0..a704784 100644 --- a/src/commands/ocr/ocr.ts +++ b/src/commands/ocr/ocr.ts @@ -6,7 +6,7 @@ import { } from "discord.js"; import { defineCommand } from ".."; import { downloadFile } from "../../utils/http"; -import { abort, sendErrorAlert } from "../../utils/error"; +import { abort } from "../../utils/error"; import { yandexOcr } from "../../utils/yandex"; import sharp from "sharp"; import { @@ -88,7 +88,7 @@ export async function ocrImpl(url: string) { const result = await lensOcr(compressed) .catch(err => { logger.error(err, "Google Lens error, falling back to Yandex"); - sendErrorAlert(err, { mime: type.mime }); + // sendErrorAlert(err, { mime: type.mime }); return yandexOcr(compressed, type.mime); }) .catch(() => abort("Failed to OCR the image")); diff --git a/src/utils/functions.ts b/src/utils/functions.ts index 613bb99..b2aa49b 100644 --- a/src/utils/functions.ts +++ b/src/utils/functions.ts @@ -9,6 +9,7 @@ import { MessageFlags, ModalSubmitInteraction, type ChatInputCommandInteraction, + type MessageSnapshot, } from "discord.js"; import { abort } from "./error"; @@ -94,15 +95,28 @@ export function getImageUrlFromChatInteraction( } export function getImageUrlFromMessage(message: Message): string { - const attachment = message.attachments.first(); + function extractImageUrl(message: Message | MessageSnapshot) { + const attachment = message.attachments.first(); - return ( - (attachment?.contentType?.startsWith("image/") && attachment.url) || - message.embeds[0]?.image?.url || - message.embeds[0]?.thumbnail?.url || - findFirstUrl(message.content) || - abort("No valid image found!") - ); + return ( + (attachment?.contentType?.startsWith("image/") && attachment.url) || + message.embeds[0]?.image?.url || + message.embeds[0]?.thumbnail?.url || + findFirstUrl(message.content) + ); + } + + if (message.messageSnapshots) { + const snapshot = message.messageSnapshots.first(); + if (snapshot) { + const url = extractImageUrl(snapshot); + if (url) return url; + } + } + + console.log("no reference url xddd"); + + return extractImageUrl(message) || abort("No valid image found!"); } export function languageCodeToName(code: string) {