From ec2ec39e25e85f1b8da15fa1f712203a27b46137 Mon Sep 17 00:00:00 2001 From: derpystuff <3515180-derpystuff@users.noreply.gitlab.com> Date: Wed, 22 Jun 2022 22:43:22 +0200 Subject: [PATCH] Context Menu OCR --- commands/interaction/context/ocr.js | 51 +++++++++++++++++++++++++++ commands/message/utils/ocr.js | 22 ++++++------ labscore/utils/attachment.js | 54 ++++++++++++++++++----------- 3 files changed, 94 insertions(+), 33 deletions(-) create mode 100644 commands/interaction/context/ocr.js diff --git a/commands/interaction/context/ocr.js b/commands/interaction/context/ocr.js new file mode 100644 index 0000000..050a2de --- /dev/null +++ b/commands/interaction/context/ocr.js @@ -0,0 +1,51 @@ +const { Constants } = require('detritus-client'); +const { InteractionCallbackTypes, ApplicationCommandTypes, MessageFlags } = Constants; + +const { googleVisionOcr } = require('../../../labscore/api'); +const { getMessageAttachment, validateAttachment } = require('../../../labscore/utils/attachment'); + +const { createEmbed } = require('../../../labscore/utils/embed'); +const { codeblock } = require('../../../labscore/utils/markdown'); +const { STATICS } = require('../../../labscore/utils/statics'); + +module.exports = { + name: 'OCR', + type: ApplicationCommandTypes.MESSAGE, + run: async (context, args) => { + try{ + await context.respond({data: { flags: MessageFlags.EPHEMERAL }, type: InteractionCallbackTypes.DEFERRED_CHANNEL_MESSAGE_WITH_SOURCE}) + + const { message } = args; + + let attachment = getMessageAttachment(message) + if(validateAttachment(attachment)){ + attachment = attachment.url + } else { + attachment = undefined + } + if(!attachment) return context.editOrRespond({ embeds: [createEmbed("warning", context, "No images found.")], flags: MessageFlags.EPHEMERAL }) + + let ocr = await googleVisionOcr(context, attachment) + + await context.editOrRespond({ + embeds: [createEmbed("default", context, { + thumbnail: { + url: attachment + }, + description: codeblock("ansi", [ocr.response.body.text]), + footer: { + iconUrl: STATICS.google, + text: `Google Cloud Vision • ${context.application.name} • Took ${ocr.timings}s` + } + })], + flags: MessageFlags.EPHEMERAL + }) + }catch(e){ + console.log(e) + await context.editOrRespond({ + embeds: [createEmbed("error", context, "Unable to perform Optical Character Recognition.")], + flags: MessageFlags.EPHEMERAL + }) + } + }, +}; \ No newline at end of file diff --git a/commands/message/utils/ocr.js b/commands/message/utils/ocr.js index bf4f1f6..bb55155 100644 --- a/commands/message/utils/ocr.js +++ b/commands/message/utils/ocr.js @@ -19,17 +19,15 @@ module.exports = { if (!image) return editOrReply(context, { embeds: [createEmbed("warning", context, "No images found.")] }) let ocr = await googleVisionOcr(context, image) - return editOrReply(context, { - embeds: [createEmbed("default", context, { - thumbnail: { - url: image - }, - description: codeblock("ansi", [ocr.response.body.text]), - footer: { - iconUrl: STATICS.google, - text: `Google Cloud Vision • ${context.application.name} • Took ${ocr.timings}s` - } - })] - }) + return editOrReply(context, createEmbed("default", context, { + thumbnail: { + url: image + }, + description: codeblock("ansi", [ocr.response.body.text]), + footer: { + iconUrl: STATICS.google, + text: `Google Cloud Vision • ${context.application.name} • Took ${ocr.timings}s` + } + })) }, }; \ No newline at end of file diff --git a/labscore/utils/attachment.js b/labscore/utils/attachment.js index 0d6d257..9f19317 100644 --- a/labscore/utils/attachment.js +++ b/labscore/utils/attachment.js @@ -2,6 +2,22 @@ const attachmentTypes = Object.freeze({ image: ["image/png", "image/jpeg", "image/gif"] }) +module.exports.attachmentTypes = attachmentTypes + +// Returns the first attachment from a message (if it exists) +function getMessageAttachment(message) { + if (message.attachments.first()) { + return message.attachments.first() + } else if (message.embeds.length > 0 && message.embeds.toArray()[0].image) { + return message.embeds.toArray()[0].image + } else if (message.embeds.length > 0 && message.embeds.toArray()[0].thumbnail) { + return message.embeds.toArray()[0].thumbnail + } + return; +} + +module.exports.getMessageAttachment = getMessageAttachment + async function getRecentMedia(context, limit) { if (!context.message.channel) { return undefined; @@ -27,20 +43,8 @@ async function getRecentMedia(context, limit) { let attachments = []; for (const m of messages) { - let message = m[1] - if ( // First the attachment on the message - message.attachments.first() - ) { - attachments.push(message.attachments.first()) - } else if ( // Then the embed image - message.embeds.length > 0 && - message.embeds.toArray()[0].image - ) { - attachments.push(message.embeds.toArray()[0].image) - } else if ( - message.embeds.length > 0 && - message.embeds.toArray()[0].thumbnail - ) { attachments.push(message.embeds.toArray()[0].thumbnail) } + let a = getMessageAttachment(m[1]) + if(a) attachments.push(a) } return attachments; } @@ -81,16 +85,24 @@ module.exports.getRecentVideo = async function(context, limit) { return attachments; } -module.exports.getRecentImage= async function(context, limit){ +function validateAttachment(attachment){ + if (attachment.contentType && attachmentTypes.image.includes(attachment.contentType)) { // discord attachment + return true + } else if (!attachment.content_type) { // other form of media + return true + } else { + return false + } +} + +module.exports.validateAttachment = validateAttachment + +module.exports.getRecentImage = async function(context, limit){ let attachments = await getRecentMedia(context, limit) + let at; - let validImages = attachmentTypes.image for (const a of attachments) { - if (a.contentType && validImages.includes(a.contentType) && at === undefined) { // discord attachment - at = a.url - } else if (!a.content_type && at === undefined) { // other form of media - at = a.url - } + if(validateAttachment(a) && at === undefined) at = a.url } return at; } \ No newline at end of file