Haha-Yes/commands/general/tts.js

61 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-12-30 01:20:24 +01:00
const { Command } = require('discord-akairo');
const textToSpeech = require('@google-cloud/text-to-speech');
const gclient = new textToSpeech.TextToSpeechClient();
2018-12-30 02:16:44 +01:00
const fs = require('fs');
2018-12-30 01:20:24 +01:00
class TtsCommand extends Command {
constructor() {
super('tts', {
aliases: ['tts'],
category: 'general',
split: 'none',
args: [
{
id: 'text',
type: 'string'
}
],
description: {
content: 'Send a mp3 of what you wrote in tts',
usage: '[text]',
examples: ['hello']
}
});
}
async exec(message, args) {
2018-12-30 02:16:00 +01:00
let text = args.text;
2019-01-02 04:35:34 +01:00
// Construct the request
const request = {
input: { text: text },
// Select the language and SSML Voice Gender (optional)
voice: { languageCode: 'en-US', ssmlGender: 'NEUTRAL' },
// Select the type of audio encoding
audioConfig: { audioEncoding: 'MP3' },
};
// Performs the Text-to-Speech request
gclient.synthesizeSpeech(request, (err, response) => {
if (err) {
fs.close();
console.error('ERROR:', err);
return;
}
// Write the binary audio content to a local file
await fs.writeFile('tts.mp3', response.audioContent, 'binary', err => {
2018-12-30 01:20:24 +01:00
if (err) {
console.error('ERROR:', err);
2019-01-02 04:35:34 +01:00
message.channel.send('An error has occured, the message is probably too long');
fs.close();
2018-12-30 01:20:24 +01:00
return;
2019-01-02 04:35:34 +01:00
}
console.log('Audio content written to file: tts.mp3');
message.channel.send({ files: ['./tts.mp3'] })
});
fs.close();
});
2018-12-30 01:20:24 +01:00
}
}
module.exports = TtsCommand;