From 2bd536aa85b31c390e239ad4cb480236ab0296f6 Mon Sep 17 00:00:00 2001 From: Supositware Date: Fri, 18 Jan 2019 22:44:16 +0100 Subject: [PATCH] work pls --- commands/general/ttsvc.js | 74 +++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/commands/general/ttsvc.js b/commands/general/ttsvc.js index 917b99d..d6b1be5 100644 --- a/commands/general/ttsvc.js +++ b/commands/general/ttsvc.js @@ -25,50 +25,48 @@ class TtsvcCommand extends Command { } async exec(message, args) { - try { - let text = args.text; + let text = args.text; - // 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) => { + // 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 => { if (err) { console.error('ERROR:', err); + message.channel.send('An error has occured, the message is probably too long'); + return; } - - // Write the binary audio content to a local file - fs.writeFile('ttsvc.mp3', response.audioContent, 'binary', async err => { - if (err) { - console.error('ERROR:', err); - message.channel.send('An error has occured, the message is probably too long'); - - return; - } - console.log('Audio content written to file: ttsvc.mp3'); - - - if (message.member.voice.channel) { - const connection = await message.member.voice.channel.join(); - const dispatcher = connection.playFile('../../ttsvc.mp3'); - dispatcher.on('finish', () => { - dispatcher.destroy(); - }); - } else { - message.reply('You need to join a voice channel first!'); - } - }); + 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(); + const dispatcher = connection.play('../../ttsvc.mp3'); + 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!`); + } }); - } catch (err) { - console.error(err); - } + }); } } module.exports = TtsvcCommand; \ No newline at end of file