You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.5 KiB
JavaScript
64 lines
1.5 KiB
JavaScript
6 years ago
|
const { Command } = require('discord-akairo');
|
||
|
const textToSpeech = require('@google-cloud/text-to-speech');
|
||
5 years ago
|
const rand = require('../../../rand.js');
|
||
6 years ago
|
const gclient = new textToSpeech.TextToSpeechClient();
|
||
6 years ago
|
const fs = require('fs');
|
||
6 years ago
|
|
||
|
class TtsCommand extends Command {
|
||
6 years ago
|
constructor() {
|
||
|
super('tts', {
|
||
|
aliases: ['tts'],
|
||
6 years ago
|
category: 'fun',
|
||
6 years ago
|
args: [
|
||
|
{
|
||
|
id: 'text',
|
||
6 years ago
|
type: 'string',
|
||
|
match: 'rest'
|
||
6 years ago
|
}
|
||
|
],
|
||
|
description: {
|
||
|
content: 'Send a mp3 of what you wrote in tts',
|
||
|
usage: '[text]',
|
||
|
examples: ['hello']
|
||
|
}
|
||
|
});
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
async exec(message, args) {
|
||
|
let text = args.text;
|
||
6 years ago
|
|
||
6 years ago
|
text = rand.random(text, message);
|
||
|
|
||
6 years ago
|
// 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' },
|
||
|
};
|
||
6 years ago
|
|
||
6 years ago
|
// Performs the Text-to-Speech request
|
||
|
gclient.synthesizeSpeech(request, (err, response) => {
|
||
|
if (err) {
|
||
6 years ago
|
|
||
6 years ago
|
console.error('ERROR:', err);
|
||
|
return;
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// Write the binary audio content to a local file
|
||
|
fs.writeFile('tts.mp3', response.audioContent, 'binary', err => {
|
||
|
if (err) {
|
||
|
console.error('ERROR:', err);
|
||
|
message.channel.send('An error has occured, the message is probably too long');
|
||
6 years ago
|
|
||
6 years ago
|
return;
|
||
|
}
|
||
|
console.log('Audio content written to file: tts.mp3');
|
||
|
message.channel.send({ files: ['./tts.mp3'] });
|
||
|
});
|
||
6 years ago
|
|
||
6 years ago
|
});
|
||
|
}
|
||
6 years ago
|
}
|
||
|
module.exports = TtsCommand;
|