Haha-Yes/commands/utility/translate.js

75 lines
2.2 KiB
JavaScript
Raw Permalink Normal View History

2018-12-30 01:20:24 +01:00
const { Command } = require('discord-akairo');
2021-08-15 23:28:56 +02:00
const axios = require('axios');
const { deepl } = 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',
2021-08-15 23:28:56 +02:00
type: ['bg', 'cs', 'da', 'de', 'el', 'en', 'es', 'et', 'fi', 'fr', 'hu', 'it', 'ja', 'lt', 'lv', 'nl', 'pl', 'pt', 'ro', 'ru', 'sk', 'sl', 'sv', 'zh'],
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) {
2021-08-15 23:28:56 +02:00
let language = args.language.toUpperCase();
2019-01-02 08:09:45 +01:00
let text = args.text;
2019-01-02 04:35:34 +01:00
2021-08-15 23:28:56 +02:00
const params = new URLSearchParams();
params.append('auth_key', deepl);
params.append('text', text);
params.append('target_lang', language);
2019-01-02 04:35:34 +01:00
2021-08-15 23:28:56 +02:00
axios.post('https://api-free.deepl.com/v2/translate', params, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(response => {
console.log(response.data.translations[0].text);
2019-01-02 04:35:34 +01:00
2021-08-15 23:28:56 +02:00
const data = response.data.translations[0];
2019-12-29 23:15:16 +01:00
2021-08-15 23:28:56 +02:00
const translationEmbed = this.client.util.embed()
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
.setTitle('Asked for the following translation:')
.setURL('https://www.deepl.com/translator')
.setAuthor(message.author.username)
.addField('Translated text', data.text)
.addField('Translated from', data.detected_source_language)
.setTimestamp()
.setFooter('Powered by DeepL');
2019-12-29 23:15:16 +01:00
2021-08-15 23:28:56 +02:00
return message.channel.send(translationEmbed);
});
2019-12-29 23:15:16 +01:00
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;