tts command using google tts

This commit is contained in:
Loic Bersier 2018-12-08 02:25:40 +01:00
parent 5cf7488cf9
commit 665f813901
3 changed files with 68 additions and 8 deletions

1
.gitignore vendored
View file

@ -9,3 +9,4 @@ json/autoresponse.json
json/blacklist.json
json/customresponse.json
tag/*
tts.mp3

View file

@ -1,7 +1,11 @@
const { Command } = require('discord.js-commando');
const speak = require('simple-tts');
const googleTTS = require('google-tts-api');
const SelfReloadJSON = require('self-reload-json');
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 {
constructor(client) {
@ -17,6 +21,7 @@ module.exports = class BadMemeCommand extends Command {
key: 'text',
prompt: 'What do you want to be said',
type: 'string',
validate: text => text.length < 201,
}
]
});
@ -27,10 +32,63 @@ module.exports = class BadMemeCommand extends Command {
if(blacklistJson[message.author.id])
return blacklist(blacklistJson[message.author.id] , message)
speak(text, {format:'mp3', filename:'./tts'})
.catch(err => message.say('An error has occured, you probably used an invalid char.'))
setTimeout(function(){
message.say({files: ['./tts.mp3']})
.catch(err => message.say('An error has occured, you probably used invalid char.'))
}, 2000)
function downloadFile (url, dest) {
return new Promise(function (resolve, reject) {
var info = urlParse(url);
var httpClient = info.protocol === 'https:' ? https : http;
var options = {
host: info.host,
path: info.path,
headers: {
'user-agent': 'WHAT_EVER'
}
};
httpClient.get(options, function(res) {
// check status code
if (res.statusCode !== 200) {
reject(new Error('request to ' + url + ' failed, status code = ' + res.statusCode + ' (' + res.statusMessage + ')'));
return;
}
var file = fs.createWriteStream(dest);
file.on('finish', function() {
// close() is async, call resolve after close completes.
file.close(resolve);
});
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(){
message.say({files: ['./tts.mp3']})
}, 2000)
}}

View file

@ -22,6 +22,7 @@
"discord.js": "^11.4.2",
"discord.js-commando": "^0.10.0",
"faceapp": "^0.4.4",
"google-tts-api": "0.0.4",
"node-fetch": "^2.2.0",
"node-gyp": "^3.8.0",
"printer": "^0.2.2",