put the vid in tmp

This commit is contained in:
loicbersier 2019-09-13 19:06:29 +02:00
parent 5dd5a73dfc
commit 39408d758a

View file

@ -2,6 +2,7 @@ const { Command } = require('discord-akairo');
const fs = require('fs'); const fs = require('fs');
const youtubedl = require('youtube-dl'); const youtubedl = require('youtube-dl');
const hbjs = require('handbrake-js'); const hbjs = require('handbrake-js');
const os = require('os');
class DownloadCommand extends Command { class DownloadCommand extends Command {
constructor() { constructor() {
@ -42,24 +43,24 @@ class DownloadCommand extends Command {
let fileName; let fileName;
if (args.spoiler) { if (args.spoiler) {
fileName = 'SPOILER_video'; fileName = `SPOILER_${message.author.id}_video`;
} else { } else {
fileName = 'video'; fileName = `${message.author.id}_video`;
} }
if (link.includes('http') || link.includes('www')) { if (link.includes('http') || link.includes('www')) {
if (args.alt) { if (args.alt) {
console.log('alt download!'); console.log('alt download!');
if (fs.existsSync(`./${fileName}.mp4`)) { if (fs.existsSync(`${os.tmpdir()}/${fileName}.mp4`)) {
fs.unlink(`./${fileName}.mp4`, (err) => { fs.unlink(`${os.tmpdir()}/${fileName}.mp4`, (err) => {
if (err); if (err);
}); });
} }
return youtubedl.exec(args.link, ['-o', `./${fileName}.mp4`], {}, function(err, output) { return youtubedl.exec(args.link, ['-o', `${os.tmpdir()}/${fileName}.mp4`], {}, function(err, output) {
if (err) throw err; if (err) throw err;
console.log(output.join('\n')); console.log(output.join('\n'));
message.delete(); message.delete();
message.channel.send(`Downloaded by ${message.author.username}`, { files: [`./${fileName}.mp4`] }) message.channel.send(`Downloaded by ${message.author.username}`, { files: [`${os.tmpdir()}/${fileName}.mp4`] })
.catch(err => { .catch(err => {
console.error(err); console.error(err);
return message.channel.send('File too big'); return message.channel.send('File too big');
@ -69,7 +70,7 @@ class DownloadCommand extends Command {
let video = youtubedl(link); let video = youtubedl(link);
video.pipe(fs.createWriteStream(`./${fileName}.mp4`)); video.pipe(fs.createWriteStream(`${os.tmpdir()}/${fileName}.mp4`));
video.on('error', function error(err) { video.on('error', function error(err) {
console.log('error 2:', err); console.log('error 2:', err);
message.channel.send('An error has occured, I can\'t download from the link you provided.'); message.channel.send('An error has occured, I can\'t download from the link you provided.');
@ -105,7 +106,7 @@ class DownloadCommand extends Command {
video.on('end', function () { video.on('end', function () {
if (!needCompress) { if (!needCompress) {
message.delete(); message.delete();
return message.channel.send(`Downloaded by ${message.author.username}`, { files: [`./${fileName}.mp4`] }) return message.channel.send(`Downloaded by ${message.author.username}`, { files: [`${os.tmpdir()}/${fileName}.mp4`] })
.catch(err => { .catch(err => {
console.error(err); console.error(err);
return message.channel.send('File too big'); return message.channel.send('File too big');
@ -140,7 +141,7 @@ class DownloadCommand extends Command {
}); });
handbrake.on('end', function () { handbrake.on('end', function () {
message.delete(); message.delete();
return message.channel.send(`Downloaded by ${message.author.username}`, { files: [`./${fileName}Ready.mp4`] }) return message.channel.send(`Downloaded by ${message.author.username}`, { files: [`${os.tmpdir()}/${fileName}Ready.mp4`] })
.catch(err => { .catch(err => {
console.error(err); console.error(err);
return message.channel.send('File too big'); return message.channel.send('File too big');