2018-12-30 01:20:24 +01:00
|
|
|
const { Command } = require('discord-akairo');
|
2018-12-09 01:38:45 +01:00
|
|
|
const textToSpeech = require('@google-cloud/text-to-speech');
|
2019-02-08 19:06:23 +01:00
|
|
|
const rand = require('../../rand.js');
|
2018-12-09 01:38:45 +01:00
|
|
|
const gclient = new textToSpeech.TextToSpeechClient();
|
2018-12-30 02:16:44 +01:00
|
|
|
const fs = require('fs');
|
2018-12-09 01:38:45 +01:00
|
|
|
|
2018-12-30 01:20:24 +01:00
|
|
|
class TtsvcCommand extends Command {
|
2019-01-02 08:09:45 +01:00
|
|
|
constructor() {
|
|
|
|
super('ttsvc', {
|
|
|
|
aliases: ['ttsvc'],
|
2019-03-30 04:43:44 +01:00
|
|
|
category: 'fun',
|
2019-01-02 08:09:45 +01:00
|
|
|
split: 'none',
|
|
|
|
args: [
|
|
|
|
{
|
|
|
|
id: 'text',
|
2019-01-10 20:45:29 +01:00
|
|
|
type: 'string',
|
|
|
|
match: 'rest'
|
2019-01-02 08:09:45 +01:00
|
|
|
}
|
|
|
|
],
|
|
|
|
description: {
|
|
|
|
content: 'Say what you wrote in voice channel',
|
|
|
|
usage: '[text]',
|
|
|
|
examples: ['hello']
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-12-09 01:38:45 +01:00
|
|
|
|
2019-01-02 08:09:45 +01:00
|
|
|
async exec(message, args) {
|
2019-01-18 22:44:16 +01:00
|
|
|
let text = args.text;
|
2019-02-08 19:06:23 +01:00
|
|
|
|
|
|
|
text = rand.random(text, message);
|
2018-12-30 02:16:00 +01:00
|
|
|
|
2019-01-18 22:44:16 +01:00
|
|
|
// Construct the request
|
|
|
|
const request = {
|
|
|
|
input: { text: text },
|
|
|
|
// Select the language and SSML Voice Gender (optional)
|
|
|
|
voice: { languageCode: 'en-US', ssmlGender: 'NEUTRAL' },
|
|
|
|
// Select the type of audio encoding
|
|
|
|
audioConfig: { audioEncoding: 'MP3' },
|
|
|
|
};
|
|
|
|
|
|
|
|
// Performs the Text-to-Speech request
|
|
|
|
gclient.synthesizeSpeech(request, (err, response) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('ERROR:', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the binary audio content to a local file
|
|
|
|
fs.writeFile('ttsvc.mp3', response.audioContent, 'binary', async err => {
|
2019-01-02 08:09:45 +01:00
|
|
|
if (err) {
|
|
|
|
console.error('ERROR:', err);
|
2019-01-18 22:44:16 +01:00
|
|
|
message.channel.send('An error has occured, the message is probably too long');
|
|
|
|
|
2019-01-02 08:09:45 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-01-18 22:44:16 +01:00
|
|
|
console.log('Audio content written to file: ttsvc.mp3');
|
|
|
|
|
|
|
|
const voiceChannel = message.member.voice.channel;
|
|
|
|
if (!voiceChannel) return message.say('Please enter a voice channel first.');
|
|
|
|
try {
|
|
|
|
const connection = await voiceChannel.join();
|
2019-01-18 22:46:58 +01:00
|
|
|
const dispatcher = connection.play('./ttsvc.mp3');
|
2019-01-18 22:44:16 +01:00
|
|
|
dispatcher.once('finish', () => voiceChannel.leave());
|
|
|
|
dispatcher.once('error', () => voiceChannel.leave());
|
|
|
|
return null;
|
|
|
|
} catch (err) {
|
|
|
|
voiceChannel.leave();
|
|
|
|
return message.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
|
|
|
}
|
2019-01-02 08:09:45 +01:00
|
|
|
});
|
2019-01-18 22:44:16 +01:00
|
|
|
});
|
2019-01-02 08:09:45 +01:00
|
|
|
}
|
2018-12-30 01:20:24 +01:00
|
|
|
}
|
|
|
|
module.exports = TtsvcCommand;
|