From 2ea818f30f7ad811621c94406b6980ee15063c6d Mon Sep 17 00:00:00 2001 From: derpystuff <3515180-derpystuff@users.noreply.gitlab.com> Date: Wed, 22 Jun 2022 22:58:44 +0200 Subject: [PATCH] Error message for ocr, fix attachment lookup --- commands/interaction/context/ocr.js | 6 ++++-- commands/message/utils/ocr.js | 5 +++++ labscore/utils/attachment.js | 10 ++++++---- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/commands/interaction/context/ocr.js b/commands/interaction/context/ocr.js index 60e3a05..21c8d4b 100644 --- a/commands/interaction/context/ocr.js +++ b/commands/interaction/context/ocr.js @@ -18,7 +18,7 @@ module.exports = { const { message } = args; let attachment = getMessageAttachment(message) - if(attachment && validateAttachment(attachment)){ + if(attachment && validateAttachment(attachment, "image")){ attachment = attachment.url } else { delete attachment; @@ -27,12 +27,14 @@ module.exports = { let ocr = await googleVisionOcr(context, attachment) + if(ocr.response.body.text.length == 0) return context.editOrRespond({ embeds: [createEmbed("warning", context, "No text detected.")], flags: MessageFlags.EPHEMERAL }) + await context.editOrRespond({ embeds: [createEmbed("default", context, { thumbnail: { url: attachment }, - description: codeblock("ansi", [ocr.response.body.text]), + description: codeblock("ansi", ["​" + ocr.response.body.text]), footer: { iconUrl: STATICS.google, text: `Google Cloud Vision • ${context.application.name} • Took ${ocr.timings}s` diff --git a/commands/message/utils/ocr.js b/commands/message/utils/ocr.js index bb55155..499672d 100644 --- a/commands/message/utils/ocr.js +++ b/commands/message/utils/ocr.js @@ -18,7 +18,12 @@ module.exports = { let image = await getRecentImage(context, 50) if (!image) return editOrReply(context, { embeds: [createEmbed("warning", context, "No images found.")] }) + console.log(image) + let ocr = await googleVisionOcr(context, image) + + if(ocr.response.body.text.length == 0) return editOrReply(context, { embeds: [createEmbed("warning", context, "No text detected.")] }) + return editOrReply(context, createEmbed("default", context, { thumbnail: { url: image diff --git a/labscore/utils/attachment.js b/labscore/utils/attachment.js index 9f19317..3cb832f 100644 --- a/labscore/utils/attachment.js +++ b/labscore/utils/attachment.js @@ -85,10 +85,12 @@ module.exports.getRecentVideo = async function(context, limit) { return attachments; } -function validateAttachment(attachment){ - if (attachment.contentType && attachmentTypes.image.includes(attachment.contentType)) { // discord attachment +function validateAttachment(attachment, type){ + console.log(attachment) + let allowedTypes = attachmentTypes[type] + if (attachment.contentType && allowedTypes.includes(attachment.contentType)) { // discord attachment return true - } else if (!attachment.content_type) { // other form of media + } else if (!attachment.contentType) { // other form of media return true } else { return false @@ -102,7 +104,7 @@ module.exports.getRecentImage = async function(context, limit){ let at; for (const a of attachments) { - if(validateAttachment(a) && at === undefined) at = a.url + if(validateAttachment(a, "image") && at === undefined) at = a.url } return at; } \ No newline at end of file