Compare commits

...

2 Commits

2 changed files with 22 additions and 10 deletions

View File

@ -6,7 +6,7 @@ import {
} from "discord.js"; } from "discord.js";
import { defineCommand } from ".."; import { defineCommand } from "..";
import { downloadFile } from "../../utils/http"; import { downloadFile } from "../../utils/http";
import { abort, sendErrorAlert } from "../../utils/error"; import { abort } from "../../utils/error";
import { yandexOcr } from "../../utils/yandex"; import { yandexOcr } from "../../utils/yandex";
import sharp from "sharp"; import sharp from "sharp";
import { import {
@ -88,7 +88,7 @@ export async function ocrImpl(url: string) {
const result = await lensOcr(compressed) const result = await lensOcr(compressed)
.catch(err => { .catch(err => {
logger.error(err, "Google Lens error, falling back to Yandex"); 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); return yandexOcr(compressed, type.mime);
}) })
.catch(() => abort("Failed to OCR the image")); .catch(() => abort("Failed to OCR the image"));

View File

@ -9,6 +9,7 @@ import {
MessageFlags, MessageFlags,
ModalSubmitInteraction, ModalSubmitInteraction,
type ChatInputCommandInteraction, type ChatInputCommandInteraction,
type MessageSnapshot,
} from "discord.js"; } from "discord.js";
import { abort } from "./error"; import { abort } from "./error";
@ -94,15 +95,26 @@ export function getImageUrlFromChatInteraction(
} }
export function getImageUrlFromMessage(message: Message): string { export function getImageUrlFromMessage(message: Message): string {
const attachment = message.attachments.first(); function extractImageUrl(message: Message | MessageSnapshot) {
const attachment = message.attachments.first();
return ( return (
(attachment?.contentType?.startsWith("image/") && attachment.url) || (attachment?.contentType?.startsWith("image/") && attachment.url) ||
message.embeds[0]?.image?.url || message.embeds[0]?.image?.url ||
message.embeds[0]?.thumbnail?.url || message.embeds[0]?.thumbnail?.url ||
findFirstUrl(message.content) || findFirstUrl(message.content)
abort("No valid image found!") );
); }
if (message.messageSnapshots) {
const snapshot = message.messageSnapshots.first();
if (snapshot) {
const url = extractImageUrl(snapshot);
if (url) return url;
}
}
return extractImageUrl(message) || abort("No valid image found!");
} }
export function languageCodeToName(code: string) { export function languageCodeToName(code: string) {