1
0
Fork 0

updated tts to use google tts

Commando
Loic Bersier 6 years ago
parent 68cfa021bb
commit a7c3598d3c

@ -1,11 +1,9 @@
const { Command } = require('discord.js-commando'); const { Command } = require('discord.js-commando');
const googleTTS = require('google-tts-api'); const textToSpeech = require('@google-cloud/text-to-speech');
const gclient = new textToSpeech.TextToSpeechClient();
const SelfReloadJSON = require('self-reload-json'); const SelfReloadJSON = require('self-reload-json');
const fs = require('fs'); const fs = require('fs');
const path = require('path');
const http = require('http');
const https = require('https');
const urlParse = require('url').parse;
module.exports = class BadMemeCommand extends Command { module.exports = class BadMemeCommand extends Command {
constructor(client) { constructor(client) {
@ -19,7 +17,6 @@ module.exports = class BadMemeCommand extends Command {
key: 'text', key: 'text',
prompt: 'What do you want to be said', prompt: 'What do you want to be said',
type: 'string', type: 'string',
validate: text => text.length < 201,
} }
] ]
}); });
@ -30,62 +27,31 @@ module.exports = class BadMemeCommand extends Command {
if(blacklistJson[message.author.id]) if(blacklistJson[message.author.id])
return blacklist(blacklistJson[message.author.id] , message) return blacklist(blacklistJson[message.author.id] , message)
function downloadFile (url, dest) { // Construct the request
return new Promise(function (resolve, reject) { const request = {
var info = urlParse(url); input: {text: text},
var httpClient = info.protocol === 'https:' ? https : http; // Select the language and SSML Voice Gender (optional)
var options = { voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
host: info.host, // Select the type of audio encoding
path: info.path, audioConfig: {audioEncoding: 'MP3'},
headers: { };
'user-agent': 'WHAT_EVER'
} // Performs the Text-to-Speech request
}; gclient.synthesizeSpeech(request, (err, response) => {
if (err) {
httpClient.get(options, function(res) { console.error('ERROR:', err);
// check status code return;
if (res.statusCode !== 200) { }
reject(new Error('request to ' + url + ' failed, status code = ' + res.statusCode + ' (' + res.statusMessage + ')'));
return; // Write the binary audio content to a local file
} fs.writeFile('tts.mp3', response.audioContent, 'binary', err => {
if (err) {
var file = fs.createWriteStream(dest); console.error('ERROR:', err);
file.on('finish', function() { return;
// close() is async, call resolve after close completes. }
file.close(resolve); console.log('Audio content written to file: tts.mp3');
});
file.on('error', function (err) {
// Delete the file async. (But we don't check the result)
fs.unlink(dest);
reject(err);
});
res.pipe(file);
})
.on('error', function(err) {
reject(err);
})
.end();
}); });
}
// start
googleTTS(text)
.then(function (url) {
console.log(url);
var dest = path.resolve(__dirname, '../../tts.mp3'); // file destination
console.log('Download to ' + dest + ' ...');
return downloadFile(url, dest);
})
.then(function () {
console.log('Download success');
})
.catch(function (err) {
console.error(err.stack);
}); });
setTimeout(function(){ setTimeout(function(){
message.say({files: ['./tts.mp3']}) message.say({files: ['./tts.mp3']})
}, 2000) }, 2000)

@ -18,6 +18,7 @@
}, },
"homepage": "https://gitlab.com/loicbersier/discordbot#readme", "homepage": "https://gitlab.com/loicbersier/discordbot#readme",
"dependencies": { "dependencies": {
"@google-cloud/text-to-speech": "^0.4.0",
"canvas": "^2.0.1", "canvas": "^2.0.1",
"discord.js": "^11.4.2", "discord.js": "^11.4.2",
"discord.js-commando": "^0.10.0", "discord.js-commando": "^0.10.0",

Loading…
Cancel
Save