mirror of
https://github.com/litruv/DAdmin.git
synced 2026-07-24 02:36:11 +10:00
Added in TTS
This commit is contained in:
BIN
1SecSilence.mp3
Normal file
BIN
1SecSilence.mp3
Normal file
Binary file not shown.
82
commands/tts.js
Normal file
82
commands/tts.js
Normal file
@@ -0,0 +1,82 @@
|
||||
const fs = require('fs');
|
||||
const textToSpeech = require('@google-cloud/text-to-speech');
|
||||
var FfmpegCommand = require('fluent-ffmpeg');
|
||||
|
||||
var pid = JSON.parse(fs.readFileSync("./googleapi.json")).project_id
|
||||
|
||||
const clientz = new textToSpeech.TextToSpeechClient({
|
||||
projectId: pid,
|
||||
keyFilename: './googleapi.json'
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
name: "Text To Speech",
|
||||
alias: ['tts'],
|
||||
helptext: "Uses google api's to run text to speech stuffs",
|
||||
helphide: false,
|
||||
permissions: ['READ_MESSAGES'],
|
||||
args: ['message'],
|
||||
category: 'general',
|
||||
command: (client, msg) => {
|
||||
if (msg.suffix == "" || msg.suffix == undefined)
|
||||
return;
|
||||
|
||||
const text = msg.suffix;
|
||||
|
||||
// Construct the request
|
||||
const request = {
|
||||
input: { text: text },
|
||||
// Select the language and SSML Voice Gender (optional)
|
||||
voice: { name: 'en-US-Wavenet-D', languageCode: 'en-US' },
|
||||
// Select the type of audio encoding
|
||||
audioConfig: { audioEncoding: 'MP3', speakingRate: '0.85' },
|
||||
};
|
||||
|
||||
// Performs the Text-to-Speech request
|
||||
clientz.synthesizeSpeech(request, (err, response) => {
|
||||
if (err) {
|
||||
console.error('ERROR:', err);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Write the binary audio content to a local file
|
||||
fs.writeFile('output.mp3', response.audioContent, 'binary', err => {
|
||||
if (err) {
|
||||
console.error('ERROR:', err);
|
||||
return;
|
||||
}
|
||||
|
||||
var command = FfmpegCommand('./output.mp3').addInput('./1SecSilence.mp3')
|
||||
.on('error', function (err) {
|
||||
console.log('An error occurred: ' + err.message);
|
||||
})
|
||||
.on('end', function () {
|
||||
msg.member.voiceChannel.join()
|
||||
.then(connection => {
|
||||
const dispatcher = connection.playFile('./merged.mp3');
|
||||
|
||||
dispatcher.on('end', () => {
|
||||
const timeoutObj = setTimeout(() => {
|
||||
const dispatcher = null;
|
||||
connection.disconnect();
|
||||
}, 60000)
|
||||
});
|
||||
|
||||
dispatcher.on('error', error => {
|
||||
console.log(error)
|
||||
});
|
||||
})
|
||||
.catch(console.error);
|
||||
})
|
||||
.mergeToFile('./merged.mp3', './tempDir');
|
||||
|
||||
|
||||
|
||||
|
||||
console.log('Audio content written to file: output.mp3');
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user