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');
|
|
|
|
const gclient = new textToSpeech.TextToSpeechClient();
|
|
|
|
|
2018-12-30 01:20:24 +01:00
|
|
|
class TtsvcCommand extends Command {
|
|
|
|
constructor() {
|
|
|
|
super('ttsvc', {
|
|
|
|
aliases: ['ttsvc'],
|
|
|
|
category: 'general',
|
|
|
|
split: 'none',
|
2018-12-09 01:38:45 +01:00
|
|
|
args: [
|
|
|
|
{
|
2018-12-30 01:20:24 +01:00
|
|
|
id: 'text',
|
|
|
|
type: 'string'
|
2018-12-09 01:38:45 +01:00
|
|
|
}
|
2018-12-30 01:20:24 +01:00
|
|
|
],
|
|
|
|
description: {
|
|
|
|
content: 'Say what you wrote in voice channel',
|
|
|
|
usage: '[text]',
|
|
|
|
examples: ['hello']
|
|
|
|
}
|
2018-12-09 01:38:45 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-12-30 01:20:24 +01:00
|
|
|
async exec(message, args) {
|
2018-12-09 01:38:45 +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
|
2018-12-09 02:13:16 +01:00
|
|
|
fs.writeFile('ttsvc.mp3', response.audioContent, 'binary', err => {
|
2018-12-09 01:38:45 +01:00
|
|
|
if (err) {
|
|
|
|
console.error('ERROR:', err);
|
|
|
|
message.say('An error has occured, the message is probably too long')
|
|
|
|
return;
|
|
|
|
}
|
2018-12-09 02:13:16 +01:00
|
|
|
console.log('Audio content written to file: ttsvc.mp3');
|
2018-12-09 01:38:45 +01:00
|
|
|
const { voiceChannel } = message.member;
|
|
|
|
|
|
|
|
// If not in voice channel ask user to join
|
|
|
|
if (!voiceChannel) {
|
|
|
|
return message.reply('please join a voice channel first!');
|
|
|
|
|
|
|
|
} else
|
|
|
|
// If user say "stop" make the bot leave voice channel
|
|
|
|
if (text == 'stop') {
|
2018-12-20 16:56:28 +01:00
|
|
|
voiceChannel.leave();
|
2018-12-09 01:38:45 +01:00
|
|
|
message.say('I leaved the channel');
|
|
|
|
} else
|
2018-12-09 02:13:16 +01:00
|
|
|
voiceChannel.join().then(connection => {
|
|
|
|
const dispatcher = connection.playStream('./ttsvc.mp3');
|
|
|
|
// End at then end of the audio stream
|
2018-12-09 02:16:53 +01:00
|
|
|
dispatcher.on('end', () => setTimeout(function(){
|
2018-12-20 16:56:28 +01:00
|
|
|
voiceChannel.leave();
|
2018-12-09 02:13:16 +01:00
|
|
|
}, 2000));
|
2018-12-09 02:10:43 +01:00
|
|
|
});
|
2018-12-09 01:38:45 +01:00
|
|
|
});
|
2018-12-09 02:13:16 +01:00
|
|
|
});
|
2018-12-30 01:20:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = TtsvcCommand;
|