Use events

This commit is contained in:
loicbersier 2020-07-11 01:23:58 +02:00
parent 5f8acc03d4
commit 284ed42afc
2 changed files with 19 additions and 17 deletions

View file

@ -56,10 +56,10 @@ class DownloadCommand extends Command {
downloader(args.link, null, `${os.tmpdir()}/${filename}.mp4`) downloader(args.link, null, `${os.tmpdir()}/${filename}.mp4`)
.catch((err) => { .on('error ', async err => {
return message.channel.send(err, { code: true }); return message.channel.send(err, { code: true });
}) })
.then(async (output) => { .on('end', async output => {
loadingmsg.delete(); loadingmsg.delete();
let file = fs.statSync(output); let file = fs.statSync(output);
let fileSize = file.size / 1000000.0; let fileSize = file.size / 1000000.0;

View file

@ -1,10 +1,12 @@
const fs = require('fs'); const fs = require('fs');
const events = require('events');
const youtubedl = require('youtube-dl'); const youtubedl = require('youtube-dl');
module.exports = function (url, option, output) { module.exports = function (url, option, output) {
return new Promise(function(resolve, reject) { let eventEmitter = new events.EventEmitter();
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.)'); 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.)\'');
if (option != null) option.push('--rm-cache-dir'); if (option != null) option.push('--rm-cache-dir');
else option = ['--rm-cache-dir']; else option = ['--rm-cache-dir'];
@ -13,14 +15,14 @@ module.exports = function (url, option, output) {
video.on('error', function error(err) { video.on('error', function error(err) {
console.log(err.toString()); console.log(err.toString());
reject(err.toString()); eventEmitter.emit('error', err.toString());
}); });
video.pipe(fs.createWriteStream(output)); video.pipe(fs.createWriteStream(output));
video.on('end', function() { video.on('end', function() {
resolve(output); eventEmitter.emit('end', output);
});
}); });
return eventEmitter;
}; };