mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 21:53:07 -04:00
98 lines
No EOL
3.6 KiB
JavaScript
98 lines
No EOL
3.6 KiB
JavaScript
const { googleVisionOcr, googleTranslate } = require('#api');
|
||
const { TRANSLATE_DISPLAY_MAPPINGS, TRANSLATE_LANGUAGES } = require('#constants');
|
||
|
||
const { getMessageAttachment, validateAttachment } = require('#utils/attachment');
|
||
const { createEmbed } = require('#utils/embed');
|
||
const { acknowledge } = require('#utils/interactions');
|
||
const { codeblock, pill, icon } = require('#utils/markdown');
|
||
const { editOrReply } = require('#utils/message');
|
||
const { STATICS, STATIC_ICONS, STATIC_ACTIONS } = require('#utils/statics');
|
||
|
||
const { ApplicationCommandTypes, MessageFlags } = require("detritus-client/lib/constants");
|
||
const { Components } = require('detritus-client/lib/utils');
|
||
|
||
module.exports = {
|
||
name: 'OCR',
|
||
type: ApplicationCommandTypes.MESSAGE,
|
||
contexts: [
|
||
0,
|
||
1,
|
||
2
|
||
],
|
||
integrationTypes: [
|
||
1
|
||
],
|
||
run: async (context, args) => {
|
||
try{
|
||
await acknowledge(context);
|
||
|
||
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))
|
||
|
||
const components = new Components({
|
||
timeout: 100000,
|
||
run: async (ctx) => {
|
||
if (ctx.userId !== context.userId) return await ctx.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE);
|
||
|
||
await ctx.editOrRespond({
|
||
embeds: [createEmbed("ai_custom", context, STATIC_ACTIONS.translate)]
|
||
})
|
||
|
||
let translate = await googleTranslate(context, ocr.response.body.text, "en", "auto")
|
||
|
||
let fromFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.from || sourceLanguage] || ''
|
||
let toFlag = TRANSLATE_DISPLAY_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] || translate.response.body.language.from || "Detected Language")} ${icon("arrow_right")} ${toFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.to] || translate.response.body.language.to)}\n${codeblock("ansi", [translate.response.body.translation])}`,
|
||
thumbnail: {
|
||
url: attachment
|
||
},
|
||
footer: {
|
||
iconUrl: STATICS.googletranslate,
|
||
text: `Google Translate • ${context.application.name}`
|
||
}
|
||
}))
|
||
}
|
||
})
|
||
|
||
components.addButton({
|
||
label: "Translate",
|
||
emoji: icon("button_translate"),
|
||
custom_id: "actions-translate",
|
||
style: 2
|
||
})
|
||
|
||
await editOrReply(context, createEmbed("default", context, {
|
||
embeds: [{
|
||
thumbnail: {
|
||
url: attachment
|
||
},
|
||
description: codeblock("ansi", ["" + ocr.response.body.text.substr(0,3900)]),
|
||
footer: {
|
||
iconUrl: STATICS.google,
|
||
text: `Google Cloud Vision • ${context.application.name} • Took ${ocr.timings}s`
|
||
}
|
||
}],
|
||
components
|
||
}))
|
||
}catch(e){
|
||
console.log(e)
|
||
await editOrReply(context, {
|
||
embeds: [createEmbed("error", context, "Unable to perform Optical Character Recognition.")],
|
||
flags: MessageFlags.EPHEMERAL
|
||
})
|
||
}
|
||
},
|
||
}; |