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
|
||||
})
|
||||
}
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue