/
video.js
64 lines (55 loc) · 2.34 KB
/
video.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { default: axios } = require('axios');
const { SlashCommandBuilder } = require('discord.js');
const Downloader = require("nodejs-file-downloader");
const fs = require('fs-extra');
const linkFileDownloader = 'https://file-downloader.net/check.php?v=';
module.exports = {
data: new SlashCommandBuilder()
.setName('video')
.setDescription('Baixa o vídeo desejado')
.addStringOption(option =>
option.setName('link')
.setDescription('Link do vídeo que deseja baixar')
.setRequired(true)),
async execute(interaction, client) {
try {
let link = interaction.options.getString('link');
let usuario = interaction.user.username;
await interaction.deferReply({ ephemeral: false });
const response = await axios.get(linkFileDownloader + link);
if (response.data.download_url === null || response.data.download_url === undefined) {
await interaction.editReply({ content: 'Não foi possível converter o vídeo', ephemeral: false });
return;
}
const downloader = new Downloader({
url: response.data.download_url,
directory: './videos',
fileName: `${usuario}.mp4`,
onProgress: function (percentage) {
console.log("% ", percentage);
},
});
try {
await downloader.download();
} catch (error) {
await interaction.editReply({ content: 'Erro ao baixar o vídeo.', ephemeral: false });
console.log(error);
return;
}
await interaction.channel.send({
files: [`./videos/${usuario}.mp4`]
});
await interaction.editReply({ content:'Baixado!', ephemeral: false });
// limpando a pasta
fs.emptyDirSync('./videos');
} catch (error) {
if (error.code === 40005) {
interaction.editReply({ content: 'Ops, vídeo muito grande para ser enviado, desculpe.', ephemeral: false });
} else {
interaction.editReply({ content: 'Ocorreu um erro ao tentar baixar o vídeo.', ephemeral: false });
}
fs.emptyDirSync('./videos');
console.log(error);
}
},
};