mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 21:53:07 -04:00
102 lines
No EOL
4.6 KiB
JavaScript
102 lines
No EOL
4.6 KiB
JavaScript
const { googleTranslate } = require('#api');
|
||
const { TRANSLATE_DISPLAY_MAPPINGS, TRANSLATE_LANGUAGES, TRANSLATE_LANGUAGE_MAPPINGS, TRANSLATE_DEFAULT_LANGUAGE_LIST, PERMISSION_GROUPS } = 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, InteractionCallbackTypes, InteractionContextTypes, ApplicationIntegrationTypes } = require("detritus-client/lib/constants");
|
||
const { Components } = require('detritus-client/lib/utils');
|
||
|
||
module.exports = {
|
||
name: 'Translate Message',
|
||
type: ApplicationCommandTypes.MESSAGE,
|
||
contexts: [
|
||
InteractionContextTypes.GUILD,
|
||
InteractionContextTypes.PRIVATE_CHANNEL,
|
||
InteractionContextTypes.BOT_DM
|
||
],
|
||
integrationTypes: [
|
||
ApplicationIntegrationTypes.USER_INSTALL
|
||
],
|
||
run: async (context, args) => {
|
||
await acknowledge(context, false, [...PERMISSION_GROUPS.baseline_slash]);
|
||
|
||
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{
|
||
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])
|
||
}
|
||
|
||
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.`))
|
||
}
|
||
},
|
||
}; |