mirror of
https://gitlab.com/bignutty/labscore.git
synced 2025-06-07 21:53:07 -04:00
adds /translate
This commit is contained in:
parent
e8312f0b32
commit
064845b307
7 changed files with 147 additions and 2 deletions
91
commands/interaction/slash/utils/translate.js
Normal file
91
commands/interaction/slash/utils/translate.js
Normal file
|
@ -0,0 +1,91 @@
|
|||
const { googleTranslate } = require('#api');
|
||||
const { TRANSLATE_LANGUAGES, TRANSLATE_DISPLAY_MAPPINGS } = require('#constants');
|
||||
|
||||
const { createEmbed } = require('#utils/embed')
|
||||
const { codeblock, icon, pill, stringwrap } = require('#utils/markdown');
|
||||
const { editOrReply } = require('#utils/message')
|
||||
const { STATICS } = require('#utils/statics');
|
||||
const { isSupported, getCodeFromAny } = require('#utils/translate');
|
||||
|
||||
const { translateLanguage } = require('#parameters').autocomplete;
|
||||
|
||||
const { ApplicationCommandOptionTypes } = require('detritus-client/lib/constants');
|
||||
const { acknowledge } = require('#utils/interactions');
|
||||
|
||||
module.exports = {
|
||||
name: 'translate',
|
||||
description: 'Translate text from and to other languages.',
|
||||
contexts: [
|
||||
0,
|
||||
1,
|
||||
2
|
||||
],
|
||||
integrationTypes: [
|
||||
1
|
||||
],
|
||||
options: [
|
||||
{
|
||||
name: 'text',
|
||||
description: 'Text to be translated.',
|
||||
type: ApplicationCommandOptionTypes.TEXT,
|
||||
required: true
|
||||
},
|
||||
{
|
||||
name: 'from',
|
||||
description: 'Source Language.',
|
||||
onAutoComplete: translateLanguage,
|
||||
required: false
|
||||
},
|
||||
{
|
||||
name: 'to',
|
||||
description: 'Target Language.',
|
||||
onAutoComplete: translateLanguage,
|
||||
required: false
|
||||
},
|
||||
{
|
||||
name: 'incognito',
|
||||
description: 'Makes the response only visible to you.',
|
||||
type: ApplicationCommandOptionTypes.BOOLEAN,
|
||||
required: false,
|
||||
default: false
|
||||
}
|
||||
],
|
||||
run: async (context, args) => {
|
||||
await acknowledge(context, args.incognito);
|
||||
|
||||
if(!args.to) args.to = "en";
|
||||
if(!args.from) args.from = "auto";
|
||||
|
||||
let content = args.text;
|
||||
|
||||
if(!content.length) return editOrReply(context, createEmbed("warning", context, "No text supplied."))
|
||||
|
||||
if(!isSupported(args.to)) return editOrReply(context, createEmbed("warning", context, `Invalid target language (${stringwrap(args.to, 10, false)}).`))
|
||||
if(!isSupported(args.from)) return editOrReply(context, createEmbed("warning", context, `Invalid source language (${stringwrap(args.from, 10, false)}).`))
|
||||
|
||||
let targetLanguage = getCodeFromAny(args.to)
|
||||
let sourceLanguage = getCodeFromAny(args.from)
|
||||
|
||||
if(!targetLanguage) return editOrReply(context, createEmbed("warning", context, `Invalid target language (${stringwrap(args.to, 10, false)}).`))
|
||||
if(!sourceLanguage) return editOrReply(context, createEmbed("warning", context, `Invalid source language (${stringwrap(args.from, 10, false)}).`))
|
||||
|
||||
try{
|
||||
let translate = await googleTranslate(context, content, targetLanguage, sourceLanguage)
|
||||
|
||||
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 || 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}`
|
||||
}
|
||||
}))
|
||||
}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.`))
|
||||
}
|
||||
}
|
||||
};
|
|
@ -64,8 +64,8 @@ module.exports = {
|
|||
return editOrReply(context, createEmbed("default", context, {
|
||||
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.google,
|
||||
text: `Google Translator • ${context.application.name}`
|
||||
iconUrl: STATICS.googletranslate,
|
||||
text: `Google Translate • ${context.application.name}`
|
||||
}
|
||||
}))
|
||||
}catch(e){
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue