From d7baadc512771702fcd5fe29a3c5e650ff766ee2 Mon Sep 17 00:00:00 2001 From: loicbersier Date: Wed, 16 Oct 2019 17:47:16 +0200 Subject: [PATCH] make the loading message always show --- commands/utility/download.js | 39 +++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/commands/utility/download.js b/commands/utility/download.js index 6daae117..57fd3654 100644 --- a/commands/utility/download.js +++ b/commands/utility/download.js @@ -49,6 +49,8 @@ class DownloadCommand extends Command { } if (link.includes('http') || link.includes('www')) { + let loadingmsg = await message.channel.send('Downloading '); + if (args.alt) { console.log('alt download!'); if (fs.existsSync(`${os.tmpdir()}/${fileName}.mp4`)) { @@ -56,13 +58,15 @@ class DownloadCommand extends Command { if (err); }); } - return youtubedl.exec(args.link, ['-o', `${os.tmpdir()}/${fileName}.mp4`], {}, function(err, output) { + return youtubedl.exec(args.link, ['-o', `${os.tmpdir()}/${fileName}.mp4`], {}, async function(err, output) { if (err) throw err; console.log(output.join('\n')); message.delete(); + loadingmsg.delete(); message.channel.send(`Downloaded by ${message.author.username}`, { files: [`${os.tmpdir()}/${fileName}.mp4`] }) .catch(err => { console.error(err); + loadingmsg.delete(); return message.channel.send('File too big'); }); }); @@ -71,18 +75,13 @@ class DownloadCommand extends Command { let video = youtubedl(link); video.pipe(fs.createWriteStream(`${os.tmpdir()}/${fileName}.mp4`)); - video.on('error', function error(err) { + video.on('error', async function error(err) { console.log('error 2:', err); + loadingmsg.delete(); message.channel.send('An error has occured, I can\'t download from the link you provided.'); }); - message.channel.send('Downloading ').then(msg => { - video.on('end', function () { - msg.delete(); - }); - }); - - video.on('info', function(info) { + video.on('info', async function(info) { let duration = info.duration; if (duration) { duration = duration.replace(/:/g, ''); @@ -90,6 +89,7 @@ class DownloadCommand extends Command { if (duration > 500) { video.pause(); video.unpipe(); + loadingmsg.delete(); return message.channel.send('Can\'t download video longer than 5 minutes'); } } @@ -103,16 +103,20 @@ class DownloadCommand extends Command { needCompress = true; } }); - video.on('end', function () { + video.on('end', async function () { if (!needCompress) { message.delete(); + loadingmsg.delete(); return message.channel.send(`Downloaded by ${message.author.username}`, { files: [`${os.tmpdir()}/${fileName}.mp4`] }) .catch(err => { console.error(err); + loadingmsg.delete(); return message.channel.send('File too big'); }); } + let compressmsg = await message.channel.send('Video bigger than 8MB compressing now (This can take a long time!)\nWant it to go faster? Donate to the dev with the donate command, so i can get a better server and do it faster!'); + const options = { input: `${os.tmpdir()}/${fileName}.mp4`, output: `${os.tmpdir()}/${fileName}compressed.mp4`, @@ -121,15 +125,10 @@ class DownloadCommand extends Command { //Compress vid if bigger than 8MB let handbrake = hbjs.spawn(options); - handbrake.on('start', function() { - message.channel.send('Video bigger than 8MB compressing now (This can take a long time!)\nWant it to go faster? Donate to the dev with the donate command, so i can get a better server and do it faster!').then(msg => { - handbrake.on('end', async function () { - await msg.delete(); - }); - }); - }); handbrake.on('error', err => { console.error(err); + loadingmsg.delete(); + compressmsg.delete(); return message.channel.send('An error has occured while compressing the video'); }); handbrake.on('progress', progress => { @@ -139,11 +138,15 @@ class DownloadCommand extends Command { progress.eta ); }); - handbrake.on('end', function () { + handbrake.on('end', async function () { message.delete(); + loadingmsg.delete(); + compressmsg.delete(); return message.channel.send(`Downloaded by ${message.author.username}`, { files: [`${os.tmpdir()}/${fileName}compressed.mp4`] }) .catch(err => { console.error(err); + loadingmsg.delete(); + compressmsg.delete(); return message.channel.send('File too big'); }); });