2018-12-30 01:20:24 +01:00
const { Command } = require ( 'discord-akairo' ) ;
2019-01-02 10:21:21 +01:00
const { MessageEmbed } = require ( 'discord.js' ) ;
2018-12-30 01:20:24 +01:00
const fetch = require ( 'node-fetch' ) ;
2018-12-02 01:46:24 +01:00
const { yandexAPI } = require ( '../../config.json' ) ;
2018-12-02 01:34:15 +01:00
2018-12-30 01:20:24 +01:00
class TranslationCommand extends Command {
2019-01-02 08:09:45 +01:00
constructor ( ) {
super ( 'translation' , {
aliases : [ 'translation' , 'trn' ] ,
category : 'utility' ,
args : [
{
id : 'language' ,
2019-01-02 10:21:21 +01:00
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' ,
2019-01-02 10:21:21 +01:00
retry : 'That\'s not a valid language! try again.'
} ,
2019-01-02 08:09:45 +01:00
default : 'en'
} ,
{
id : 'text' ,
type : 'string' ,
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]' ,
2019-01-02 10:21:21 +01:00
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 ) ;
fetch ( ` https://translate.yandex.net/api/v1.5/tr.json/translate?key= ${ yandexAPI } &text= ${ textURI } &lang= ${ language } &options=1 ` , {
} ) . then ( ( response ) => {
return response . json ( ) ;
} ) . then ( ( response ) => {
if ( response . code == '502' )
return message . channel . send ( ` ${ response . message } , you probably didin't input the correct language code, you can check them here! https://tech.yandex.com/translate/doc/dg/concepts/api-overview-docpage/ ` ) ;
else if ( response . code != '200' )
return message . channel . send ( 'An error has occured' ) ;
2019-01-02 04:35:34 +01:00
2019-01-02 10:21:21 +01:00
const translationEmbed = new MessageEmbed ( )
2019-01-02 08:09:45 +01:00
. setColor ( '#0099ff' )
. setTitle ( 'Asked for the following translation:' )
. setAuthor ( message . author . username )
. setDescription ( response . text [ 0 ] )
. addField ( 'Original text' , text )
. addField ( 'Translated from' , response . detected . lang )
. setTimestamp ( )
. setFooter ( 'Powered by Yandex.Translate ' ) ;
2019-01-02 04:35:34 +01:00
2019-01-02 08:09:45 +01:00
message . channel . send ( translationEmbed ) ;
} ) ;
}
2018-12-30 01:20:24 +01:00
}
module . exports = TranslationCommand ;