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.
75 lines
2.0 KiB
JavaScript
75 lines
2.0 KiB
JavaScript
6 years ago
|
const { Command } = require('discord-akairo');
|
||
6 years ago
|
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
|
|
||
6 years ago
|
class TtsvcCommand extends Command {
|
||
6 years ago
|
constructor() {
|
||
|
super('ttsvc', {
|
||
|
aliases: ['ttsvc'],
|
||
6 years ago
|
category: 'fun',
|
||
6 years ago
|
split: 'none',
|
||
|
args: [
|
||
|
{
|
||
|
id: 'text',
|
||
6 years ago
|
type: 'string',
|
||
|
match: 'rest'
|
||
6 years ago
|
}
|
||
|
],
|
||
|
description: {
|
||
|
content: 'Say what you wrote in voice channel',
|
||
|
usage: '[text]',
|
||
|
examples: ['hello']
|
||
|
}
|
||
|
});
|
||
|
}
|
||
6 years ago
|
|
||
6 years ago
|
async exec(message, args) {
|
||
6 years ago
|
let text = args.text;
|
||
6 years ago
|
|
||
|
text = rand.random(text, message);
|
||
6 years ago
|
|
||
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' },
|
||
|
};
|
||
|
|
||
|
// Performs the Text-to-Speech request
|
||
|
gclient.synthesizeSpeech(request, (err, response) => {
|
||
|
if (err) {
|
||
|
console.error('ERROR:', err);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Write the binary audio content to a local file
|
||
|
fs.writeFile('ttsvc.mp3', response.audioContent, 'binary', async err => {
|
||
6 years ago
|
if (err) {
|
||
|
console.error('ERROR:', err);
|
||
6 years ago
|
message.channel.send('An error has occured, the message is probably too long');
|
||
|
|
||
6 years ago
|
return;
|
||
|
}
|
||
6 years ago
|
console.log('Audio content written to file: ttsvc.mp3');
|
||
|
|
||
|
const voiceChannel = message.member.voice.channel;
|
||
|
if (!voiceChannel) return message.say('Please enter a voice channel first.');
|
||
|
try {
|
||
|
const connection = await voiceChannel.join();
|
||
6 years ago
|
const dispatcher = connection.play('./ttsvc.mp3');
|
||
6 years ago
|
dispatcher.once('finish', () => voiceChannel.leave());
|
||
|
dispatcher.once('error', () => voiceChannel.leave());
|
||
|
return null;
|
||
|
} catch (err) {
|
||
|
voiceChannel.leave();
|
||
|
return message.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
|
||
|
}
|
||
6 years ago
|
});
|
||
6 years ago
|
});
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
module.exports = TtsvcCommand;
|