mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-08 06:03:04 -04:00
Context Menu OCR
This commit is contained in:
parent
e37de9e68b
commit
ec2ec39e25
3 changed files with 94 additions and 33 deletions
51
commands/interaction/context/ocr.js
Normal file
51
commands/interaction/context/ocr.js
Normal 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
|
||||
})
|
||||
}
|
||||
},
|
||||
};
|
|
@ -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`
|
||||
}
|
||||
}))
|
||||
},
|
||||
};
|
|
@ -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;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue