forked from Supositware/Haha-Yes
updated tts to use google tts
This commit is contained in:
parent
68cfa021bb
commit
a7c3598d3c
2 changed files with 28 additions and 61 deletions
|
@ -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'
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
httpClient.get(options, function(res) {
|
// Performs the Text-to-Speech request
|
||||||
// check status code
|
gclient.synthesizeSpeech(request, (err, response) => {
|
||||||
if (res.statusCode !== 200) {
|
if (err) {
|
||||||
reject(new Error('request to ' + url + ' failed, status code = ' + res.statusCode + ' (' + res.statusMessage + ')'));
|
console.error('ERROR:', err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var file = fs.createWriteStream(dest);
|
// Write the binary audio content to a local file
|
||||||
file.on('finish', function() {
|
fs.writeFile('tts.mp3', response.audioContent, 'binary', err => {
|
||||||
// close() is async, call resolve after close completes.
|
if (err) {
|
||||||
file.close(resolve);
|
console.error('ERROR:', err);
|
||||||
});
|
return;
|
||||||
file.on('error', function (err) {
|
}
|
||||||
// Delete the file async. (But we don't check the result)
|
console.log('Audio content written to file: tts.mp3');
|
||||||
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…
Reference in a new issue