mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-08 22:23:03 -04:00
ocrtr
This commit is contained in:
parent
7805aeccda
commit
a5e2f79a46
2 changed files with 54 additions and 3 deletions
54
commands/message/utils/ocrtr.js
Normal file
54
commands/message/utils/ocrtr.js
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
const { googleVisionOcr, googleTranslate } = require("../../../labscore/api");
|
||||||
|
const { getRecentImage } = require("../../../labscore/utils/attachment");
|
||||||
|
const { createEmbed } = require("../../../labscore/utils/embed");
|
||||||
|
const { codeblock, icon, highlight } = require("../../../labscore/utils/markdown");
|
||||||
|
const { editOrReply } = require("../../../labscore/utils/message");
|
||||||
|
const { STATICS } = require("../../../labscore/utils/statics");
|
||||||
|
const { isSupported } = require("../../../labscore/utils/translate");
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
name: 'ocrtr',
|
||||||
|
label: 'to',
|
||||||
|
metadata: {
|
||||||
|
description: 'Optical Character Recognition + Translate',
|
||||||
|
examples: ['ocrtr en'],
|
||||||
|
category: 'utils',
|
||||||
|
usage: 'ocrtr <language> [-from <origin language>]'
|
||||||
|
},
|
||||||
|
args: [
|
||||||
|
{name: 'from', default: 'auto'}
|
||||||
|
],
|
||||||
|
run: async (context, args) => {
|
||||||
|
context.triggerTyping();
|
||||||
|
|
||||||
|
if(!args.to) args.to = "en"
|
||||||
|
|
||||||
|
if(!isSupported(args.to)) return editOrReply(context, createEmbed("warning", context, "Invalid language (to)."))
|
||||||
|
if(!isSupported(args.from)) return editOrReply(context, createEmbed("warning", context, "Invalid language (from)."))
|
||||||
|
|
||||||
|
let image = await getRecentImage(context, 50)
|
||||||
|
if (!image) return editOrReply(context, { embeds: [createEmbed("warning", context, "No images found.")] })
|
||||||
|
|
||||||
|
let ocr = await googleVisionOcr(context, image)
|
||||||
|
|
||||||
|
if(ocr.response.body.status == 1) return editOrReply(context, { embeds: [createEmbed("warning", context, ocr.response.body.message)] })
|
||||||
|
|
||||||
|
try{
|
||||||
|
let translate = await googleTranslate(context, ocr.response.body.text, args.to, args.from)
|
||||||
|
return editOrReply(context, createEmbed("default", context, {
|
||||||
|
description: `${icon("locale")} ${highlight(`${translate.response.body.language.from} -> ${translate.response.body.language.to}`)}\n${codeblock("ansi", [translate.response.body.translation])}`,
|
||||||
|
thumbnail: {
|
||||||
|
url: image
|
||||||
|
},
|
||||||
|
footer: {
|
||||||
|
iconUrl: STATICS.google,
|
||||||
|
text: `Google Cloud Vision • ${context.application.name} • Took ${parseFloat(ocr.timings) + parseFloat(translate.timings)}s`
|
||||||
|
}
|
||||||
|
}))
|
||||||
|
}catch(e){
|
||||||
|
console.log(e)
|
||||||
|
if(e.response?.body?.status && e.response.body.status == 2) return editOrReply(context, {embeds:[createEmbed("error", context, `Unable to translate text.`)]})
|
||||||
|
return editOrReply(context, {embeds:[createEmbed("error", context, `Something went wrong.`)]})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
|
@ -1,11 +1,8 @@
|
||||||
const { createEmbed } = require('../../../labscore/utils/embed')
|
const { createEmbed } = require('../../../labscore/utils/embed')
|
||||||
const { editOrReply } = require('../../../labscore/utils/message')
|
const { editOrReply } = require('../../../labscore/utils/message')
|
||||||
|
|
||||||
const { getRecentImage } = require("../../../labscore/utils/attachment");
|
|
||||||
const { codeblock, highlight, icon } = require('../../../labscore/utils/markdown');
|
const { codeblock, highlight, icon } = require('../../../labscore/utils/markdown');
|
||||||
|
|
||||||
const superagent = require('superagent');
|
|
||||||
|
|
||||||
const { isSupported } = require('../../../labscore/utils/translate');
|
const { isSupported } = require('../../../labscore/utils/translate');
|
||||||
const { googleTranslate } = require('../../../labscore/api');
|
const { googleTranslate } = require('../../../labscore/api');
|
||||||
const { STATICS } = require('../../../labscore/utils/statics');
|
const { STATICS } = require('../../../labscore/utils/statics');
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue