From 2cb9e3ae09acbf5a262c6a476280cf3965ba24a1 Mon Sep 17 00:00:00 2001 From: Loic Bersier Date: Sun, 9 Dec 2018 01:38:45 +0100 Subject: [PATCH] tts vc --- commands/fun/ttsvc.js | 74 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 commands/fun/ttsvc.js diff --git a/commands/fun/ttsvc.js b/commands/fun/ttsvc.js new file mode 100644 index 00000000..a3b92013 --- /dev/null +++ b/commands/fun/ttsvc.js @@ -0,0 +1,74 @@ +const { Command } = require('discord.js-commando'); +const textToSpeech = require('@google-cloud/text-to-speech'); +const gclient = new textToSpeech.TextToSpeechClient(); +const SelfReloadJSON = require('self-reload-json'); +const fs = require('fs'); + + +module.exports = class ttsvcCommand extends Command { + constructor(client) { + super(client, { + name: 'ttsvc', + group: 'fun', + memberName: 'ttsvc', + description: `Play what you write in tts in vc`, + args: [ + { + key: 'text', + prompt: 'What do you want to be said', + type: 'string', + } + ] + }); + } + + async run(message, { text }) { + let blacklistJson = new SelfReloadJSON('./json/blacklist.json'); + if(blacklistJson[message.author.id]) + return blacklist(blacklistJson[message.author.id] , message) + + // 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('tts.mp3', response.audioContent, 'binary', err => { + if (err) { + console.error('ERROR:', err); + message.say('An error has occured, the message is probably too long') + return; + } + console.log('Audio content written to file: tts.mp3'); + const { voiceChannel } = message.member; + + // If not in voice channel ask user to join + if (!voiceChannel) { + return message.reply('please join a voice channel first!'); + + } else + // If user say "stop" make the bot leave voice channel + if (text == 'stop') { + voiceChannel.leave() + message.say('I leaved the channel'); + } else + voiceChannel.join().then(connection => { + const stream = "./tts.mp3" + const dispatcher = connection.playStream('./tts.mp3'); + // End at then end of the audio stream + dispatcher.on('end', () => voiceChannel.leave()); + }); + }); + }); +}} \ No newline at end of file