From 284ed42afc82878371f4d02feac3d0d67709ebdc Mon Sep 17 00:00:00 2001 From: loicbersier Date: Sat, 11 Jul 2020 01:23:58 +0200 Subject: [PATCH] Use events --- commands/utility/download.js | 4 ++-- utils/download.js | 32 +++++++++++++++++--------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/commands/utility/download.js b/commands/utility/download.js index 667d04cd..b7ea21b9 100644 --- a/commands/utility/download.js +++ b/commands/utility/download.js @@ -56,10 +56,10 @@ class DownloadCommand extends Command { downloader(args.link, null, `${os.tmpdir()}/${filename}.mp4`) - .catch((err) => { + .on('error ', async err => { return message.channel.send(err, { code: true }); }) - .then(async (output) => { + .on('end', async output => { loadingmsg.delete(); let file = fs.statSync(output); let fileSize = file.size / 1000000.0; diff --git a/utils/download.js b/utils/download.js index 0a088d42..7c1855ce 100644 --- a/utils/download.js +++ b/utils/download.js @@ -1,26 +1,28 @@ const fs = require('fs'); +const events = require('events'); const youtubedl = require('youtube-dl'); module.exports = function (url, option, output) { - return new Promise(function(resolve, reject) { - if (!url) reject('Require a url!'); - if (!output) reject('Require an output parameter! (If you see this message please send a feedback to the dev about it.)'); + let eventEmitter = new events.EventEmitter(); - if (option != null) option.push('--rm-cache-dir'); - else option = ['--rm-cache-dir']; + if (!url) eventEmitter.emit('error', 'Require an url!'); + if (!output) eventEmitter.emit('error', 'Require an output parameter! (If you see this message please send a feedback to the dev about it.)\''); - const video = youtubedl(url, option); + if (option != null) option.push('--rm-cache-dir'); + else option = ['--rm-cache-dir']; - video.on('error', function error(err) { - console.log(err.toString()); - reject(err.toString()); - }); + const video = youtubedl(url, option); - video.pipe(fs.createWriteStream(output)); - - video.on('end', function() { - resolve(output); - }); + video.on('error', function error(err) { + console.log(err.toString()); + eventEmitter.emit('error', err.toString()); }); + + video.pipe(fs.createWriteStream(output)); + + video.on('end', function() { + eventEmitter.emit('end', output); + }); + return eventEmitter; };