update translate header design (again, this time consistently)

This commit is contained in:
bignutty 2025-02-22 15:20:11 +01:00
parent e2075b8306
commit c5d49cf0f7
5 changed files with 109 additions and 69 deletions

View file

@ -4,12 +4,12 @@ const { TRANSLATE_DISPLAY_MAPPINGS, TRANSLATE_LANGUAGES, PERMISSION_GROUPS } = r
const { getMessageAttachment, validateAttachment } = require('#utils/attachment'); const { getMessageAttachment, validateAttachment } = require('#utils/attachment');
const { createEmbed } = require('#utils/embed'); const { createEmbed } = require('#utils/embed');
const { acknowledge } = require('#utils/interactions'); const { acknowledge } = require('#utils/interactions');
const { codeblock, pill, icon, iconAsEmojiObject } = require('#utils/markdown'); const { codeblock, pill, icon, stringwrap} = require('#utils/markdown');
const { editOrReply } = require('#utils/message'); const { editOrReply } = require('#utils/message');
const { STATICS, STATIC_ACTIONS } = require('#utils/statics'); const { STATICS } = require('#utils/statics');
const { Context, Argument } = require('detritus-client/lib/command'); const { Context, Argument } = require('detritus-client/lib/command');
const { ApplicationCommandTypes, MessageFlags, InteractionContextTypes, ApplicationIntegrationTypes, MessageComponentTypes, InteractionCallbackTypes } = require("detritus-client/lib/constants"); const { ApplicationCommandTypes, MessageFlags, InteractionContextTypes, ApplicationIntegrationTypes, InteractionCallbackTypes } = require("detritus-client/lib/constants");
const { Components } = require('detritus-client/lib/utils'); const { Components } = require('detritus-client/lib/utils');
module.exports = { module.exports = {
@ -39,13 +39,13 @@ module.exports = {
if(attachment && validateAttachment(attachment, "image")){ if(attachment && validateAttachment(attachment, "image")){
attachment = attachment.url attachment = attachment.url
} else { } else {
delete attachment; attachment = false;
} }
if(!attachment) return editOrReply(context, createEmbed("warning", context, "No images found.")) if(!attachment) return editOrReply(context, createEmbed("warning", context, "No images found."))
let ocr = await googleVisionOcr(context, attachment) let ocr = await googleVisionOcr(context, attachment)
if(ocr.response.body.status == 1) return editOrReply(context, createEmbed("warning", context, ocr.response.body.text)) if(ocr.response.body.status === 1) return editOrReply(context, createEmbed("warning", context, ocr.response.body.text))
const components = new Components({ const components = new Components({
timeout: 100000, timeout: 100000,
@ -54,11 +54,11 @@ module.exports = {
let translate = await googleTranslate(context, ocr.response.body.text, "en", "auto") let translate = await googleTranslate(context, ocr.response.body.text, "en", "auto")
let fromFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.from || sourceLanguage] || '' let fromFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.from] || ''
let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || '' let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || ''
return editOrReply(context, createEmbed("default", context, { 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])}`, description: `-# ${icon("locale")} ${fromFlag} ${pill(TRANSLATE_LANGUAGES[translate.response.body.language.from] || 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", [stringwrap(translate.response.body.translation, 1900)])}`,
thumbnail: { thumbnail: {
url: attachment url: attachment
}, },

View file

@ -1,14 +1,20 @@
const { googleVisionOcr } = require("#api"); const {googleVisionOcr, googleTranslate} = require("#api");
const { PERMISSION_GROUPS } = require("#constants"); const {PERMISSION_GROUPS, TRANSLATE_DISPLAY_MAPPINGS, TRANSLATE_LANGUAGES} = require("#constants");
const {validateAttachment} = require("#utils/attachment"); const {validateAttachment} = require("#utils/attachment");
const {createEmbed} = require("#utils/embed"); const {createEmbed} = require("#utils/embed");
const {acknowledge} = require("#utils/interactions"); const {acknowledge} = require("#utils/interactions");
const { codeblock, stringwrap } = require("#utils/markdown"); const {codeblock, stringwrap, icon, pill} = require("#utils/markdown");
const {editOrReply} = require("#utils/message"); const {editOrReply} = require("#utils/message");
const {STATICS} = require("#utils/statics"); const {STATICS} = require("#utils/statics");
const { ApplicationCommandOptionTypes, InteractionContextTypes, ApplicationIntegrationTypes } = require('detritus-client/lib/constants'); const {
ApplicationCommandOptionTypes,
InteractionContextTypes,
ApplicationIntegrationTypes,
InteractionCallbackTypes
} = require('detritus-client/lib/constants');
const {Components} = require("detritus-client/lib/utils");
module.exports = { module.exports = {
name: 'ocr', name: 'ocr',
@ -44,9 +50,41 @@ module.exports = {
try { try {
let ocr = await googleVisionOcr(context, args.image.url); 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)] }) if (ocr.response.body.status === 1) return context.editOrRespond({embeds: [createEmbed("warning", context, ocr.response.body.text)]})
return await editOrReply(context, createEmbed("default", context, { const components = new Components({
timeout: 100000,
run: async (ctx) => {
if (ctx.userId !== context.userId) return await ctx.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE);
let translate = await googleTranslate(context, ocr.response.body.text, "en", "auto")
let fromFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.from] || ''
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] || 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", [stringwrap(translate.response.body.translation, 1900)])}`,
thumbnail: {
url: args.image.url
},
footer: {
iconUrl: STATICS.googletranslate,
text: `Google Translate • ${context.application.name}`
}
}))
}
})
components.addButton({
label: "Translate",
emoji: icon("button_translate"),
custom_id: "actions-translate",
style: 2
})
return await editOrReply(context, {
embeds: [createEmbed(
"default", context, {
thumbnail: { thumbnail: {
url: args.image.url url: args.image.url
}, },
@ -55,7 +93,9 @@ module.exports = {
iconUrl: STATICS.googlelens, iconUrl: STATICS.googlelens,
text: `Google Lens • ${context.application.name} • Took ${ocr.timings}s` text: `Google Lens • ${context.application.name} • Took ${ocr.timings}s`
} }
})) })],
components
})
} catch (e) { } catch (e) {
console.log(e) console.log(e)
return editOrReply(context, createEmbed("error", context, "Unable to perform OCR.")) return editOrReply(context, createEmbed("error", context, "Unable to perform OCR."))

View file

@ -76,14 +76,14 @@ module.exports = {
let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || '' let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || ''
return editOrReply(context, createEmbed("default", context, { return editOrReply(context, createEmbed("default", context, {
description: `-# ${icon("subtext_translate")} Translated from ${fromFlag} **${TRANSLATE_LANGUAGES[translate.response.body.language.from || sourceLanguage] || translate.response.body.language.from || args.from}** to ${toFlag} **${TRANSLATE_LANGUAGES[translate.response.body.language.to] || translate.response.body.language.to}**.\n${codeblock("ansi", [translate.response.body.translation])}`, 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])}`,
footer: { footer: {
iconUrl: STATICS.googletranslate, iconUrl: STATICS.googletranslate,
text: `Google Translate • ${context.application.name}` text: `Google Translate • ${context.application.name}`
} }
})) }))
}catch(e){ }catch(e){
if(e.response?.body?.status && e.response.body.status == 2) return editOrReply(context, createEmbed("error", context, `Unable to translate text.`)) if(e.response?.body?.status && e.response.body.status === 2) return editOrReply(context, createEmbed("error", context, `Unable to translate text.`))
console.log(e) console.log(e)
return editOrReply(context, createEmbed("error", context, `Something went wrong.`)) return editOrReply(context, createEmbed("error", context, `Something went wrong.`))
} }

View file

@ -47,7 +47,7 @@ module.exports = {
return editOrReply(context, createEmbed("error", context, "Unable to retrieve Google Vision API response.")) return editOrReply(context, createEmbed("error", context, "Unable to retrieve Google Vision API response."))
} }
if(ocr.response.body.status == 1) return editOrReply(context, createEmbed("warning", context, ocr.response.body.text)) if(ocr.response.body.status === 1) return editOrReply(context, createEmbed("warning", context, ocr.response.body.text))
try{ try{
let translate = await googleTranslate(context, ocr.response.body.text, targetLanguage, sourceLanguage) let translate = await googleTranslate(context, ocr.response.body.text, targetLanguage, sourceLanguage)
@ -56,7 +56,7 @@ module.exports = {
let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || '' let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || ''
return editOrReply(context, createEmbed("default", context, { 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,4000)])}`, 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,4000)])}`,
thumbnail: { thumbnail: {
url: image url: image
}, },
@ -66,7 +66,7 @@ module.exports = {
} }
})) }))
}catch(e){ }catch(e){
if(e.response?.body?.status && e.response.body.status == 2) return editOrReply(context, createEmbed("error", context, `Unable to translate text.`)) if(e.response?.body?.status && e.response.body.status === 2) return editOrReply(context, createEmbed("error", context, `Unable to translate text.`))
console.log(e) console.log(e)
return editOrReply(context, createEmbed("error", context, `Something went wrong.`)) return editOrReply(context, createEmbed("error", context, `Something went wrong.`))
} }

View file

@ -181,7 +181,7 @@ module.exports = {
let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || '' let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.response.body.language.to] || ''
return editOrReply(context, createEmbed("default", context, { return editOrReply(context, createEmbed("default", context, {
description: `-# ${icon("subtext_translate")} Translated from ${fromFlag} **${TRANSLATE_LANGUAGES[translate.response.body.language.from || sourceLanguage] || translate.response.body.language.from || args.from}** to ${toFlag} **${TRANSLATE_LANGUAGES[translate.response.body.language.to] || translate.response.body.language.to}**.\n${codeblock("ansi", [translate.response.body.translation])}`, 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", [stringwrap(translate.response.body.translation, 1900)])}`,
footer: { footer: {
iconUrl: STATICS.googletranslate, iconUrl: STATICS.googletranslate,
text: `Google Translate • ${context.application.name}` text: `Google Translate • ${context.application.name}`