Haha-Yes/commands/fun/tts/tts.js

70 lines
1.7 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');
2019-05-18 03:01:57 +02:00
const rand = require('../../../rand.js');
2018-12-30 01:20:24 +01:00
const gclient = new textToSpeech.TextToSpeechClient();
2018-12-30 02:16:44 +01:00
const fs = require('fs');
2020-02-01 02:36:48 +01:00
const os = require('os');
2018-12-30 01:20:24 +01:00
class TtsCommand extends Command {
2019-01-02 08:09:45 +01:00
constructor() {
super('tts', {
aliases: ['tts'],
2019-03-30 04:43:44 +01:00
category: 'fun',
2019-11-09 12:04:01 +01:00
clientPermissions: ['ATTACH_FILES'],
2019-01-02 08:09:45 +01:00
args: [
{
id: 'text',
2019-01-10 20:45:29 +01:00
type: 'string',
2019-06-23 03:41:59 +02:00
prompt: {
start: 'Write something so i can say it back in Google tts',
},
2019-01-10 20:45:29 +01:00
match: 'rest'
2019-01-02 08:09:45 +01:00
}
],
description: {
content: 'Send a mp3 of what you wrote in tts',
usage: '[text]',
examples: ['hello']
}
});
}
2018-12-30 01:20:24 +01:00
2019-01-02 08:09:45 +01:00
async exec(message, args) {
let text = args.text;
2020-02-01 02:36:48 +01:00
let output = `${os.tmpdir()}/${message.id}_tts.mp3`;
2019-01-02 04:35:34 +01:00
2019-02-08 19:06:23 +01:00
text = rand.random(text, message);
2019-01-02 08:09:45 +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' },
};
2019-01-02 04:35:34 +01:00
2019-01-02 08:09:45 +01:00
// Performs the Text-to-Speech request
gclient.synthesizeSpeech(request, (err, response) => {
if (err) {
2019-01-02 10:48:26 +01:00
2019-01-02 08:09:45 +01:00
console.error('ERROR:', err);
return;
}
2019-01-02 04:35:34 +01:00
2019-01-02 08:09:45 +01:00
// Write the binary audio content to a local file
2020-02-01 02:36:48 +01:00
fs.writeFile(output, response.audioContent, 'binary', err => {
2019-01-02 08:09:45 +01:00
if (err) {
console.error('ERROR:', err);
message.channel.send('An error has occured, the message is probably too long');
2019-01-02 10:48:26 +01:00
2019-01-02 08:09:45 +01:00
return;
}
console.log('Audio content written to file: tts.mp3');
2020-02-01 02:36:48 +01:00
message.channel.send({ files: [output] });
2019-01-02 08:09:45 +01:00
});
2019-01-02 10:48:26 +01:00
2019-01-02 08:09:45 +01:00
});
}
2018-12-30 01:20:24 +01:00
}
module.exports = TtsCommand;