- do not translate non-rich embeds (fixes gifs, other issues)

- add placeholder for unknown languages in translate ops
This commit is contained in:
bignutty 2025-03-20 08:51:38 +01:00
parent 3f550a4822
commit d9c2db1510
3 changed files with 24 additions and 7 deletions

View file

@ -6,7 +6,9 @@ const { acknowledge } = require('#utils/interactions');
const { icon, stringwrap } = require('#utils/markdown');
const { editOrReply } = require('#utils/message');
const { ApplicationCommandTypes, InteractionCallbackTypes, InteractionContextTypes, ApplicationIntegrationTypes } = require("detritus-client/lib/constants");
const { ApplicationCommandTypes, InteractionCallbackTypes, InteractionContextTypes, ApplicationIntegrationTypes,
MessageEmbedTypes
} = require("detritus-client/lib/constants");
const { Components } = require('detritus-client/lib/utils');
async function translateMessage(context, message, to, from){
@ -19,12 +21,13 @@ async function translateMessage(context, message, to, from){
cnt.shift();
if(cnt.length >= 1){ mappings.content = cnt.join("\n") }
} else mappings.content = message.content
};
}
if(message.embeds) {
let i = 0;
// Message Translation supports Descriptions and Fields
for(const e of message.embeds){
let emb = e[1]
if(e[1].type !== MessageEmbedTypes.RICH) continue;
if(emb.description) mappings["embeds/" + i + "/description"] = emb.description;
if(emb.title) mappings["embeds/" + i + "/title"] = emb.title;
if(emb.author?.name) mappings["embeds/" + i + "/author/name"] = emb.author.name;
@ -43,7 +46,7 @@ async function translateMessage(context, message, to, from){
}
// Cancel if we don't have anything that can be translated
if(Object.keys(mappings).length == 0) return {};
if(Object.keys(mappings).length === 0) return {};
// Translate message via multitranslate endpoint on 1p
try{
@ -63,6 +66,7 @@ async function translateMessage(context, message, to, from){
// Message Translation supports Descriptions and Fields
for(const e of message.embeds){
let emb = e[1]
if(e[1].type !== MessageEmbedTypes.RICH) continue;
let newEmbed = {
fields: []
};
@ -151,7 +155,7 @@ module.exports = {
let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.metadata.language.to] || ''
let newMessage = translate.message;
let newMessageContent = "";;
let newMessageContent = "";
if(newMessage.content) newMessageContent += "\n" + newMessage.content
return await ctx.editOrRespond({
@ -185,7 +189,7 @@ module.exports = {
let toFlag = TRANSLATE_DISPLAY_MAPPINGS[translate.metadata.language.to] || ''
let newMessage = translate.message;
let newMessageContent = "";;
let newMessageContent = "";
if(newMessage.content) newMessageContent += "\n" + newMessage.content
return editOrReply(context, createEmbed("default", context, {
@ -194,7 +198,7 @@ module.exports = {
components
}))
}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)
return editOrReply(context, createEmbed("error", context, `Something went wrong.`))
}