pissbot-9000/commands/interaction/context/ocrtr.js
2024-07-28 17:25:59 +02:00

68 lines
No EOL
2.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const { googleVisionOcr, googleTranslate } = require('#api');
const { TRANSLATE_LANGUAGE_MAPPINGS, TRANSLATE_LANGUAGES } = require('#constants');
const { getMessageAttachment, validateAttachment } = require('#utils/attachment');
const { createEmbed } = require('#utils/embed');
const { acknowledge } = require('#utils/interactions');
const { editOrReply } = require('#utils/message');
const { codeblock, icon, pill } = require('#utils/markdown');
const { STATICS } = require('#utils/statics');
const { ApplicationCommandTypes, MessageFlags } = require("detritus-client/lib/constants");
module.exports = {
name: 'OCR Translate',
type: ApplicationCommandTypes.MESSAGE,
contexts: [
0,
1,
2
],
integrationTypes: [
1
],
run: async (context, args) => {
await acknowledge(context);
try{
const { message } = args;
let attachment = getMessageAttachment(message)
if(attachment && validateAttachment(attachment, "image")){
attachment = attachment.url
} else {
delete attachment;
}
if(!attachment) return editOrReply(context, createEmbed("warning", context, "No images found."))
let ocr = await googleVisionOcr(context, attachment)
if(ocr.response.body.status == 1) return editOrReply(context, createEmbed("warning", context, ocr.response.body.text))
try{
let translate = await googleTranslate(context, ocr.response.body.text, "en", "auto")
let fromFlag = TRANSLATE_LANGUAGE_MAPPINGS[translate.response.body.language.from || sourceLanguage] || ''
let toFlag = TRANSLATE_LANGUAGE_MAPPINGS[translate.response.body.language.to] || ''
return editOrReply(context, createEmbed("default", context, {
description: `${icon("locale")} ${fromFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.from || sourceLanguage])} ${icon("arrow_right")} ${toFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.to])}\n${codeblock("ansi", [translate.response.body.translation.substr(0,3900)])}`,
thumbnail: {
url: attachment
},
footer: {
iconUrl: STATICS.google,
text: `Google Cloud Vision • ${context.application.name}`
}
}))
}catch(e){
console.log(e)
if(e.response?.body?.status && e.response.body.status == 2) return editOrReply(context, createEmbed("error", context, `Unable to translate text.`))
return editOrReply(context, createEmbed("error", context, `Something went wrong.`))
}
}catch(e){
console.log(e)
await editOrReply(context, createEmbed("error", context, "Unable to perform Optical Character Recognition."))
}
},
};