diff --git a/.gitignore b/.gitignore index d0b56d42..a2ba5ca7 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ json/autoresponse.json json/blacklist.json json/customresponse.json tag/* +tts.mp3 diff --git a/commands/fun/tts.js b/commands/fun/tts.js index a0e94308..8f1bfb21 100644 --- a/commands/fun/tts.js +++ b/commands/fun/tts.js @@ -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) }} diff --git a/package.json b/package.json index 35142221..7205f16a 100644 --- a/package.json +++ b/package.json @@ -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",