Context Menu OCR

This commit is contained in:
derpystuff 2022-06-22 22:43:22 +02:00
parent e37de9e68b
commit ec2ec39e25
3 changed files with 94 additions and 33 deletions

View file

@ -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
})
}
},
};

View file

@ -19,8 +19,7 @@ module.exports = {
if (!image) return editOrReply(context, { embeds: [createEmbed("warning", context, "No images found.")] }) if (!image) return editOrReply(context, { embeds: [createEmbed("warning", context, "No images found.")] })
let ocr = await googleVisionOcr(context, image) let ocr = await googleVisionOcr(context, image)
return editOrReply(context, { return editOrReply(context, createEmbed("default", context, {
embeds: [createEmbed("default", context, {
thumbnail: { thumbnail: {
url: image url: image
}, },
@ -29,7 +28,6 @@ module.exports = {
iconUrl: STATICS.google, iconUrl: STATICS.google,
text: `Google Cloud Vision • ${context.application.name} • Took ${ocr.timings}s` text: `Google Cloud Vision • ${context.application.name} • Took ${ocr.timings}s`
} }
})] }))
})
}, },
}; };

View file

@ -2,6 +2,22 @@ const attachmentTypes = Object.freeze({
image: ["image/png", "image/jpeg", "image/gif"] 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) { async function getRecentMedia(context, limit) {
if (!context.message.channel) { if (!context.message.channel) {
return undefined; return undefined;
@ -27,20 +43,8 @@ async function getRecentMedia(context, limit) {
let attachments = []; let attachments = [];
for (const m of messages) { for (const m of messages) {
let message = m[1] let a = getMessageAttachment(m[1])
if ( // First the attachment on the message if(a) attachments.push(a)
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) }
} }
return attachments; return attachments;
} }
@ -81,16 +85,24 @@ module.exports.getRecentVideo = async function(context, limit) {
return attachments; return attachments;
} }
module.exports.getRecentImage= async function(context, limit){ function validateAttachment(attachment){
let attachments = await getRecentMedia(context, limit) if (attachment.contentType && attachmentTypes.image.includes(attachment.contentType)) { // discord attachment
let at; return true
let validImages = attachmentTypes.image } else if (!attachment.content_type) { // other form of media
for (const a of attachments) { return true
if (a.contentType && validImages.includes(a.contentType) && at === undefined) { // discord attachment } else {
at = a.url return false
} else if (!a.content_type && at === undefined) { // other form of media
at = a.url
} }
}
module.exports.validateAttachment = validateAttachment
module.exports.getRecentImage = async function(context, limit){
let attachments = await getRecentMedia(context, limit)
let at;
for (const a of attachments) {
if(validateAttachment(a) && at === undefined) at = a.url
} }
return at; return at;
} }