From fd173b3653eb5e2855cd648ba407a0c7c8e76762 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Mon, 15 Jun 2020 16:55:08 +0200 Subject: [PATCH] Send a message when an error occur --- commands/fun/tts/sam.js | 19 ++++++++++------- commands/fun/tts/samvc.js | 44 ++++++++++++++++++++++----------------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/commands/fun/tts/sam.js b/commands/fun/tts/sam.js index 55cd5cf9..a8e7a436 100644 --- a/commands/fun/tts/sam.js +++ b/commands/fun/tts/sam.js @@ -30,7 +30,7 @@ class samCommand extends Command { async exec(message, args) { args.samMessage = rand.random(args.samMessage, message); - let pitch = ''; + let pitch; if (args.samMessage.includes('[pitch:')) { pitch = args.samMessage.split(/(\[pitch:.*?])/); for (let i = 0, l = pitch.length; i < l; i++) { @@ -48,7 +48,7 @@ class samCommand extends Command { pitch = 100; } - let speed = ''; + let speed; if (args.samMessage.includes('[speed:')) { speed = args.samMessage.split(/(\[speed:.*?])/); for (let i = 0, l = speed.length; i < l; i++) { @@ -76,11 +76,16 @@ class samCommand extends Command { headers: { 'Content-Type': 'audio/mpeg', }, - }).then((result) => { - const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`; - fs.writeFileSync(outputFilename, result.data); - return message.channel.send({files: [outputFilename]}); - }); + }) + .catch((err) => { + console.error(err); + return message.channel.send(`Uh oh, an error has occured! please try again later.\n${err}`); + }) + .then((result) => { + const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`; + fs.writeFileSync(outputFilename, result.data); + return message.channel.send({files: [outputFilename]}); + }); } } diff --git a/commands/fun/tts/samvc.js b/commands/fun/tts/samvc.js index 3c05776f..73a6f5d6 100644 --- a/commands/fun/tts/samvc.js +++ b/commands/fun/tts/samvc.js @@ -30,7 +30,7 @@ class samvcCommand extends Command { async exec(message, args) { args.samMessage = rand.random(args.samMessage, message); - let pitch = ''; + let pitch; if (args.samMessage.includes('[pitch:')) { pitch = args.samMessage.split(/(\[pitch:.*?])/); for (let i = 0, l = pitch.length; i < l; i++) { @@ -48,7 +48,7 @@ class samvcCommand extends Command { pitch = 100; } - let speed = ''; + let speed; if (args.samMessage.includes('[speed:')) { speed = args.samMessage.split(/(\[speed:.*?])/); for (let i = 0, l = speed.length; i < l; i++) { @@ -76,25 +76,31 @@ class samvcCommand extends Command { headers: { 'Content-Type': 'audio/mpeg', }, - }).then(async (result) => { - const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`; + }) + .catch((err) => { + console.error(err); + return message.channel.send(`Uh oh, an error has occured! please try again later.\n${err}`); + }) - fs.writeFile(outputFilename, result.data, async function(err) { - if (err) console.error(err); - const voiceChannel = message.member.voice.channel; - if (!voiceChannel) return message.channel.send('Please enter a voice channel first.'); - try { - const connection = await voiceChannel.join(); - const dispatcher = connection.play(outputFilename); - 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}\`.`); - } + .then(async (result) => { + const outputFilename = `${os.tmpdir}/${message.id}_sam.wav`; + + fs.writeFile(outputFilename, result.data, async function(err) { + if (err) console.error(err); + const voiceChannel = message.member.voice.channel; + if (!voiceChannel) return message.channel.send('Please enter a voice channel first.'); + try { + const connection = await voiceChannel.join(); + const dispatcher = connection.play(outputFilename); + 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}\`.`); + } + }); }); - }); } }