Haha-Yes/commands/utility/translate.js

105 lines
4.2 KiB
JavaScript
Raw Normal View History

2018-12-30 01:20:24 +01:00
const { Command } = require('discord-akairo');
const fetch = require('node-fetch');
2019-12-29 23:15:16 +01:00
const { yandexTRN, yandexDICT } = require('../../config.json');
2018-12-02 01:34:15 +01:00
2020-03-19 16:32:09 +01:00
class translateCommand extends Command {
2019-01-02 08:09:45 +01:00
constructor() {
2020-03-19 16:32:09 +01:00
super('translate', {
aliases: ['translate', 'trn'],
2019-01-02 08:09:45 +01:00
category: 'utility',
2019-11-09 12:04:01 +01:00
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS'],
2019-01-02 08:09:45 +01:00
args: [
{
id: 'language',
type: ['az','ml','sq','mt','am','mk','en','mi','ar','mr','hy','mhr','af','mn','eu','de','ba','ne','be','no','bn','pa','my','pap','bg','fa','bs','pl','cy','pt','hu','ro','vi','ru','ht','ceb','gl','sr','nl','si','mrj','sk','el','sl','ka','sw','gu','su','da','tg','he','th','yi','tl','id','ta','ga','tt','it','te','is','tr','es','udm','kk','uz','kn','uk','ca','ur','ky','fi','zh','fr','ko','hi','xh','hr','km','cs','lo','sv','la','gd','lv','et','lt','eo','lb','jv','mg','ja','ms'],
prompt: {
2019-06-23 03:41:59 +02:00
start: 'In what language do you want to translate it to',
retry: 'That\'s not a valid language! try again.'
},
2019-01-02 08:09:45 +01:00
default: 'en'
},
{
id: 'text',
type: 'string',
2019-11-09 11:21:24 +01:00
prompt: {
start: 'What sentences/words do you want to translate?',
},
2019-01-14 18:24:25 +01:00
match: 'rest'
2019-01-02 08:09:45 +01:00
}
],
description: {
content: 'Translate what you send in your desired language. You can find the language code here: https://tech.yandex.com/translate/doc/dg/concepts/api-overview-docpage/',
usage: '[language code] [Text to translate]',
examples: ['fr "What are we doing today?"', 'en "Que faisons-nous aujourd\'hui?"']
2019-01-02 08:09:45 +01:00
}
});
}
2018-12-02 01:34:15 +01:00
2019-01-02 08:09:45 +01:00
async exec(message, args) {
let language = args.language;
let text = args.text;
2019-01-02 04:35:34 +01:00
2019-01-02 08:09:45 +01:00
let textURI = encodeURI(text);
2019-12-29 23:15:16 +01:00
fetch(`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${yandexTRN}&text=${textURI}&lang=${language}&options=1`, {
2019-01-02 08:09:45 +01:00
}).then((response) => {
return response.json();
}).then((response) => {
if (response.code == '502')
2019-08-08 21:24:41 +02:00
return message.channel.send(`${response.message}, you probably didn't input the correct language code, you can check them here! https://tech.yandex.com/translate/doc/dg/concepts/api-overview-docpage/`);
2019-01-02 08:09:45 +01:00
else if (response.code != '200')
return message.channel.send('An error has occurred');
2019-12-29 23:15:16 +01:00
let description = response.text[0];
let lang = response.detected.lang;
2019-01-02 04:35:34 +01:00
2019-12-29 23:15:16 +01:00
// Lookup in yandex dictionary the translated word and find other word for it
fetch(`https://dictionary.yandex.net/api/v1/dicservice.json/lookup?key=${yandexDICT}&lang=${language}-${language}&text=${encodeURI(response.text[0])}`, {
}).then((response) => {
return response.json();
}).then((response) => {
console.log(response);
if (response.code == '501') return message.channel.send(response.message);
// If it didn't find anything in the dictionary simply send the translation
if (response.def.length == 0) {
const translationEmbed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
2019-12-29 23:15:16 +01:00
.setTitle('Asked for the following translation:')
.setURL('https://tech.yandex.com/dictionary/')
.setAuthor(message.author.username)
.addField('Translated text', description)
.addField('Translated from', lang)
.setTimestamp()
.setFooter('Powered by Yandex.Translate');
2019-01-02 04:35:34 +01:00
2019-12-29 23:15:16 +01:00
return message.channel.send(translationEmbed);
}
let dict = '';
if (response.def[0].ts == undefined)
description = `${response.def[0].pos} - ${response.def[0].text}`;
else
description = `${response.def[0].pos} - ${response.def[0].text} - [${response.def[0].ts}]`;
for (let i = 0; i < response.def[0].tr.length; i++ ) {
dict += `\n${response.def[0].tr[i].pos} - ${response.def[0].tr[i].text} `;
}
const translationEmbed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
2019-12-29 23:15:16 +01:00
.setTitle('Asked for the following translation:')
.setURL('https://tech.yandex.com/dictionary/')
.setAuthor(message.author.username)
.addField('Translated text', description)
.addField('Dictionary', dict)
.addField('Translated from', lang)
.setTimestamp()
.setFooter('Powered by Yandex.Translate & Yandex.Dictionary');
return message.channel.send(translationEmbed);
});
2019-01-02 04:35:34 +01:00
2019-01-02 08:09:45 +01:00
});
}
2018-12-30 01:20:24 +01:00
}
2020-03-19 16:32:09 +01:00
module.exports = translateCommand;