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`)
|
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;
|
||||||
|
|
|
@ -1,26 +1,28 @@
|
||||||
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 (option != null) option.push('--rm-cache-dir');
|
if (!url) eventEmitter.emit('error', 'Require an url!');
|
||||||
else option = ['--rm-cache-dir'];
|
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) {
|
const video = youtubedl(url, option);
|
||||||
console.log(err.toString());
|
|
||||||
reject(err.toString());
|
|
||||||
});
|
|
||||||
|
|
||||||
video.pipe(fs.createWriteStream(output));
|
video.on('error', function error(err) {
|
||||||
|
console.log(err.toString());
|
||||||
video.on('end', function() {
|
eventEmitter.emit('error', err.toString());
|
||||||
resolve(output);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
video.pipe(fs.createWriteStream(output));
|
||||||
|
|
||||||
|
video.on('end', function() {
|
||||||
|
eventEmitter.emit('end', output);
|
||||||
|
});
|
||||||
|
return eventEmitter;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue