mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 13:43:06 -04:00
64 lines
No EOL
2 KiB
JavaScript
64 lines
No EOL
2 KiB
JavaScript
const { googleVisionOcr } = require("#api");
|
||
const { PERMISSION_GROUPS } = require("#constants");
|
||
|
||
const { validateAttachment } = require("#utils/attachment");
|
||
const { createEmbed } = require("#utils/embed");
|
||
const { acknowledge } = require("#utils/interactions");
|
||
const { codeblock, stringwrap } = require("#utils/markdown");
|
||
const { editOrReply } = require("#utils/message");
|
||
const { STATICS } = require("#utils/statics");
|
||
|
||
const { ApplicationCommandOptionTypes } = require('detritus-client/lib/constants');
|
||
|
||
module.exports = {
|
||
name: 'ocr',
|
||
description: 'Reads text contents from an image.',
|
||
contexts: [
|
||
0,
|
||
1,
|
||
2
|
||
],
|
||
integrationTypes: [
|
||
1
|
||
],
|
||
options: [
|
||
{
|
||
name: 'image',
|
||
description: 'Image to scan',
|
||
type: ApplicationCommandOptionTypes.ATTACHMENT,
|
||
required: true
|
||
},
|
||
{
|
||
name: 'incognito',
|
||
description: 'Makes the response only visible to you.',
|
||
type: ApplicationCommandOptionTypes.BOOLEAN,
|
||
required: false,
|
||
default: false
|
||
}
|
||
],
|
||
run: async (context, args) => {
|
||
await acknowledge(context, args.incognito, [...PERMISSION_GROUPS.baseline_slash]);
|
||
|
||
if(!validateAttachment(args.image, "image")) return await editOrReply(context, createEmbed("warning", context, "Unsupported attachment type."))
|
||
|
||
try{
|
||
let ocr = await googleVisionOcr(context, args.image.url);
|
||
|
||
if(ocr.response.body.status == 1) return context.editOrRespond({ embeds: [createEmbed("warning", context, ocr.response.body.text)] })
|
||
|
||
return await editOrReply(context, createEmbed("default", context, {
|
||
thumbnail: {
|
||
url: args.image.url
|
||
},
|
||
description: codeblock("ansi", ["" + stringwrap(ocr.response.body.text, 2000)]),
|
||
footer: {
|
||
iconUrl: STATICS.googlelens,
|
||
text: `Google Lens • ${context.application.name} • Took ${ocr.timings}s`
|
||
}
|
||
}))
|
||
}catch(e){
|
||
console.log(e)
|
||
return editOrReply(context, createEmbed("error", context, "Unable to perform OCR."))
|
||
}
|
||
}
|
||
} |