Use 'url' type for link argument, added a retry prompt and modified when loadingmsg get deleted

This commit is contained in:
loicbersier 2020-07-16 09:23:50 +02:00
parent ed805a9a0f
commit 1767a04ecb

View file

@ -4,7 +4,6 @@ const compress = require('../../utils/compress');
const os = require('os'); const os = require('os');
const fs = require('fs'); const fs = require('fs');
class DownloadCommand extends Command { class DownloadCommand extends Command {
constructor() { constructor() {
super('download', { super('download', {
@ -14,10 +13,11 @@ class DownloadCommand extends Command {
args: [ args: [
{ {
id: 'link', id: 'link',
type: 'string', type: 'url',
prompt: { prompt: {
start: 'Send the link of which video you want to download', start: 'Please send the URL of which video you want to download. Say `cancel` to stop the command',
} retry: 'Please send a valid URL of the video you want to download. Say `cancel` to stop the command'
},
}, },
{ {
id: 'caption', id: 'caption',
@ -55,16 +55,16 @@ class DownloadCommand extends Command {
downloader(args.link, null, `${os.tmpdir()}/${filename}.mp4`) downloader(args.link.href, null, `${os.tmpdir()}/${filename}.mp4`)
.on('error ', async err => { .on('error', async err => {
return message.channel.send(err, { code: true }); return message.channel.send(err, { code: true });
}) })
.on('end', async output => { .on('end', async output => {
loadingmsg.delete();
let file = fs.statSync(output); let file = fs.statSync(output);
let fileSize = file.size / 1000000.0; let fileSize = file.size / 1000000.0;
if (fileSize > 8) { if (fileSize > 8) {
loadingmsg.delete();
let compressEmbed = this.client.util.embed() let compressEmbed = this.client.util.embed()
.setColor(message.member ? message.member.displayHexColor : 'NAVY') .setColor(message.member ? message.member.displayHexColor : 'NAVY')
.setTitle('This one will need compression!') .setTitle('This one will need compression!')
@ -97,7 +97,6 @@ class DownloadCommand extends Command {
handbrake.on('end', (output) => { handbrake.on('end', (output) => {
clearInterval(editmsg); clearInterval(editmsg);
compressmsg.delete();
file = fs.statSync(output); file = fs.statSync(output);
fileSize = file.size / 1000000.0; fileSize = file.size / 1000000.0;
@ -105,10 +104,12 @@ class DownloadCommand extends Command {
return message.channel.send({embed: Embed, files: [output]}) return message.channel.send({embed: Embed, files: [output]})
.catch(err => { .catch(err => {
compressmsg.delete();
console.error(err); console.error(err);
return message.channel.send('File too big'); return message.channel.send(`${err.name}: ${err.message} ${err.message === 'Request entity too large' ? 'The file size is too big' : ''}`);
}) })
.then(() => { .then(() => {
compressmsg.delete();
// Delete file after it has been sent // Delete file after it has been sent
fs.unlinkSync(output); fs.unlinkSync(output);
message.delete(); message.delete();
@ -117,10 +118,12 @@ class DownloadCommand extends Command {
} else { } else {
return message.channel.send({embed: Embed, files: [output]}) return message.channel.send({embed: Embed, files: [output]})
.catch(err => { .catch(err => {
loadingmsg.delete();
console.error(err); console.error(err);
return message.channel.send('File too big'); return message.channel.send(`${err.name}: ${err.message} ${err.message === 'Request entity too large' ? 'The file size is too big' : ''}`);
}) })
.then(() => { .then(() => {
loadingmsg.delete();
// Delete file after it has been sent // Delete file after it has been sent
fs.unlinkSync(output); fs.unlinkSync(output);
message.delete(); message.delete();