forked from Supositware/Haha-Yes
Use events
This commit is contained in:
parent
5f8acc03d4
commit
284ed42afc
2 changed files with 19 additions and 17 deletions
|
@ -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;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
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 (!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');
|
||||
else option = ['--rm-cache-dir'];
|
||||
|
@ -13,14 +15,14 @@ module.exports = function (url, option, output) {
|
|||
|
||||
video.on('error', function error(err) {
|
||||
console.log(err.toString());
|
||||
reject(err.toString());
|
||||
eventEmitter.emit('error', err.toString());
|
||||
});
|
||||
|
||||
video.pipe(fs.createWriteStream(output));
|
||||
|
||||
video.on('end', function() {
|
||||
resolve(output);
|
||||
});
|
||||
eventEmitter.emit('end', output);
|
||||
});
|
||||
return eventEmitter;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue