mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 13:43:06 -04:00
adds ctx translate
This commit is contained in:
parent
064845b307
commit
28af9c5cb5
4 changed files with 129 additions and 88 deletions
|
@ -1,68 +0,0 @@
|
|||
const { googleVisionOcr, googleTranslate } = require('#api');
|
||||
const { TRANSLATE_LANGUAGES, TRANSLATE_DISPLAY_MAPPINGS } = 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_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])} ${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."))
|
||||
}
|
||||
},
|
||||
};
|
105
commands/interaction/context/translate.js
Normal file
105
commands/interaction/context/translate.js
Normal file
|
@ -0,0 +1,105 @@
|
|||
const { googleTranslate } = require('#api');
|
||||
const { TRANSLATE_DISPLAY_MAPPINGS, TRANSLATE_LANGUAGES, TRANSLATE_LANGUAGE_MAPPINGS, TRANSLATE_DEFAULT_LANGUAGE_LIST } = require('#constants');
|
||||
|
||||
const { createEmbed } = require('#utils/embed');
|
||||
const { acknowledge } = require('#utils/interactions');
|
||||
const { codeblock, icon, pill } = require('#utils/markdown');
|
||||
const { editOrReply } = require('#utils/message');
|
||||
const { STATICS } = require('#utils/statics');
|
||||
|
||||
const { ApplicationCommandTypes } = require("detritus-client/lib/constants");
|
||||
const { Components } = require('detritus-client/lib/utils');
|
||||
|
||||
module.exports = {
|
||||
name: 'Translate Message',
|
||||
type: ApplicationCommandTypes.MESSAGE,
|
||||
contexts: [
|
||||
0,
|
||||
1,
|
||||
2
|
||||
],
|
||||
integrationTypes: [
|
||||
1
|
||||
],
|
||||
run: async (context, args) => {
|
||||
await acknowledge(context);
|
||||
|
||||
const { message } = args;
|
||||
|
||||
if(!message.content) return editOrReply(context, createEmbed("warning", context, "No content found."))
|
||||
|
||||
try{
|
||||
let translate = await googleTranslate(context, message.content, "en", "auto")
|
||||
|
||||
let fromFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.from || sourceLanguage] || ''
|
||||
let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || ''
|
||||
|
||||
|
||||
const components = new Components({
|
||||
timeout: 100000,
|
||||
run: async (ctx) => {
|
||||
|
||||
try{
|
||||
console.log("translating")
|
||||
if (ctx.userId !== context.userId) return await ctx.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE);
|
||||
|
||||
let translate = await googleTranslate(context, message.content, ctx.data.values[0], "auto")
|
||||
|
||||
let fromFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.from || sourceLanguage] || ''
|
||||
let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || ''
|
||||
|
||||
for (let i = 0; i < components.components[0].components[0].options.length; i++) {
|
||||
components.components[0].components[0].options[i].default = (components.components[0].components[0].options[i].value === ctx.data.values[0])
|
||||
}
|
||||
|
||||
console.log("translated")
|
||||
|
||||
await ctx.editOrRespond({
|
||||
embeds: [{
|
||||
description: `${icon("locale")} ${fromFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.from || sourceLanguage] || translate.response.body.language.from || args.from)} ${icon("arrow_right")} ${toFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.to] || translate.response.body.language.to)}\n${codeblock("ansi", [translate.response.body.translation])}`,
|
||||
footer: {
|
||||
iconUrl: STATICS.googletranslate,
|
||||
text: `Google Translate • ${context.application.name}`
|
||||
}
|
||||
}],
|
||||
components
|
||||
})
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let selectLanguageOptions = TRANSLATE_DEFAULT_LANGUAGE_LIST.map((r, i) => {
|
||||
return {
|
||||
label: TRANSLATE_LANGUAGES[r],
|
||||
value: r,
|
||||
emoji: TRANSLATE_LANGUAGE_MAPPINGS[r] || undefined,
|
||||
default: !i
|
||||
}
|
||||
})
|
||||
|
||||
components.addSelectMenu({
|
||||
defaultValues: [],
|
||||
placeholder: "Change target language",
|
||||
customId: "target-language",
|
||||
options: selectLanguageOptions
|
||||
})
|
||||
|
||||
return editOrReply(context, createEmbed("default", context, {
|
||||
embeds: [{
|
||||
description: `${icon("locale")} ${fromFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.from || sourceLanguage] || translate.response.body.language.from || args.from)} ${icon("arrow_right")} ${toFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.to] || translate.response.body.language.to)}\n${codeblock("ansi", [translate.response.body.translation])}`,
|
||||
footer: {
|
||||
iconUrl: STATICS.googletranslate,
|
||||
text: `Google Translate • ${context.application.name}`
|
||||
}
|
||||
}],
|
||||
components
|
||||
}))
|
||||
}catch(e){
|
||||
if(e.response?.body?.status && e.response.body.status == 2) return editOrReply(context, createEmbed("error", context, `Unable to translate text.`))
|
||||
console.log(e)
|
||||
return editOrReply(context, createEmbed("error", context, `Something went wrong.`))
|
||||
}
|
||||
},
|
||||
};
|
|
@ -693,6 +693,27 @@ module.exports.DICTIONARY_LANGUAGES = Object.freeze({
|
|||
zh: "Chinese"
|
||||
})
|
||||
|
||||
// Default list of languages uses in various translate contexts
|
||||
module.exports.TRANSLATE_DEFAULT_LANGUAGE_LIST = Object.freeze([
|
||||
"en",
|
||||
"de",
|
||||
"fr",
|
||||
"es",
|
||||
"pt-PT",
|
||||
"no",
|
||||
"fi",
|
||||
"sv",
|
||||
"it",
|
||||
"ja",
|
||||
"ru",
|
||||
"ar",
|
||||
"he",
|
||||
"tr",
|
||||
"hi",
|
||||
"zh-CN",
|
||||
"zh-TW",
|
||||
])
|
||||
|
||||
// hey if you're looking at this and are annoyed that a language
|
||||
// doesn't as expected work lmk on discord @bignutty and i'll add
|
||||
// it (within reason), same goes for flag emoji below
|
||||
|
@ -1044,6 +1065,7 @@ module.exports.TRANSLATE_LANGUAGE_MAPPINGS = Object.freeze({
|
|||
"pl": "🇵🇱",
|
||||
"pt": "🇵🇹",
|
||||
"pt-pt": "🇵🇹",
|
||||
"pt-PT": "🇵🇹",
|
||||
"pt-br": "🇧🇷",
|
||||
"ro": "🇷🇴",
|
||||
"ru": "🇷🇺",
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const { TRANSLATE_LANGUAGES } = require("#constants");
|
||||
const { TRANSLATE_LANGUAGES, TRANSLATE_DEFAULT_LANGUAGE_LIST } = require("#constants");
|
||||
const { getLanguagesFromAny } = require("#utils/translate");
|
||||
|
||||
module.exports = async (context)=>{
|
||||
|
@ -7,25 +7,7 @@ module.exports = async (context)=>{
|
|||
choices = getLanguagesFromAny(context.value)
|
||||
} else {
|
||||
// Default language suggestions
|
||||
choices = [
|
||||
"en",
|
||||
"de",
|
||||
"fr",
|
||||
"es",
|
||||
"pt-PT",
|
||||
"no",
|
||||
"fi",
|
||||
"sv",
|
||||
"it",
|
||||
"ja",
|
||||
"ru",
|
||||
"ar",
|
||||
"he",
|
||||
"tr",
|
||||
"hi",
|
||||
"zh-CN",
|
||||
"zh-TW",
|
||||
]
|
||||
choices = TRANSLATE_DEFAULT_LANGUAGE_LIST
|
||||
}
|
||||
|
||||
return context.respond({ choices: choices.splice(0, 20).map((l)=>({
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue