2022-06-17 01:25:05 +02:00
|
|
|
import { SlashCommandBuilder } from '@discordjs/builders';
|
|
|
|
import { MessageEmbed, MessageActionRow, MessageSelectMenu } from 'discord.js';
|
|
|
|
import { exec } from 'node:child_process';
|
|
|
|
import fs from 'node:fs';
|
|
|
|
import os from 'node:os';
|
2022-06-17 07:31:58 +02:00
|
|
|
import utils from '../../utils/videos.js';
|
2022-06-17 01:25:05 +02:00
|
|
|
|
|
|
|
export default {
|
2022-06-16 09:18:39 +02:00
|
|
|
data: new SlashCommandBuilder()
|
|
|
|
.setName('download')
|
|
|
|
.setDescription('Download a video.')
|
|
|
|
.addStringOption(option =>
|
|
|
|
option.setName('url')
|
|
|
|
.setDescription('URL of the video you want to download.')
|
|
|
|
.setRequired(true))
|
|
|
|
.addBooleanOption(option =>
|
|
|
|
option.setName('advanced')
|
|
|
|
.setDescription('Choose the quality of the video.')
|
|
|
|
.setRequired(false)),
|
|
|
|
|
|
|
|
async execute(interaction) {
|
|
|
|
await interaction.deferReply({ ephemeral: false });
|
|
|
|
const url = interaction.options.getString('url');
|
|
|
|
|
2022-06-16 23:11:01 +02:00
|
|
|
const urlRE = new RegExp('([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+@)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?([^ ])+');
|
|
|
|
if (!url.match(urlRE)) {
|
|
|
|
return interaction.editReply({ content: '❌ This does not look like a valid URL!', ephemeral: true });
|
|
|
|
}
|
|
|
|
|
2022-06-16 09:18:39 +02:00
|
|
|
if (interaction.options.getBoolean('advanced')) {
|
|
|
|
let qualitys = await new Promise((resolve, reject) => {
|
|
|
|
exec(`./bin/yt-dlp ${url} --print "%()j"`, (err, stdout, stderr) => {
|
|
|
|
if (err) {
|
|
|
|
reject(stderr);
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.error(stderr);
|
|
|
|
}
|
|
|
|
resolve(stdout);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
qualitys = JSON.parse(qualitys);
|
|
|
|
|
|
|
|
const options = [];
|
|
|
|
|
|
|
|
qualitys.formats.forEach(f => {
|
|
|
|
options.push({
|
|
|
|
label: f.resolution,
|
2022-06-16 14:28:49 +02:00
|
|
|
description: `${f.format} V: ${f.vcodec} A: ${f.acodec}`,
|
2022-06-16 09:18:39 +02:00
|
|
|
value: f.format_id,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
if (options.length > 25) {
|
|
|
|
// Reverse so the higher quality formats are first
|
|
|
|
options.reverse();
|
|
|
|
while (options.length > 25) {
|
|
|
|
// Remove the lower quality formats
|
|
|
|
options.pop();
|
|
|
|
}
|
|
|
|
// Reverse again so the lower quality appears first
|
|
|
|
options.reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
const row = new MessageActionRow()
|
|
|
|
.addComponents(
|
|
|
|
new MessageSelectMenu()
|
|
|
|
.setCustomId('downloadQuality')
|
|
|
|
.setPlaceholder('Nothing selected')
|
2022-06-16 14:36:38 +02:00
|
|
|
.setMinValues(1)
|
|
|
|
.setMaxValues(2)
|
2022-06-16 09:18:39 +02:00
|
|
|
.addOptions(options),
|
|
|
|
);
|
|
|
|
|
|
|
|
await interaction.deleteReply();
|
|
|
|
await interaction.followUp({ content: 'Which quality do you want?', ephemeral: true, components: [row] });
|
|
|
|
|
2022-06-16 10:25:45 +02:00
|
|
|
interaction.client.once('interactionCreate', async (interactionMenu) => {
|
2022-06-16 09:18:39 +02:00
|
|
|
if (!interactionMenu.isSelectMenu()) return;
|
|
|
|
if (interactionMenu.customId === 'downloadQuality') {
|
|
|
|
await interactionMenu.deferReply({ ephemeral: false });
|
|
|
|
download(url, interactionMenu);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
download(url, interaction);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
async function download(url, interaction) {
|
|
|
|
let format = 'bestvideo*+bestaudio/best';
|
|
|
|
const Embed = new MessageEmbed()
|
|
|
|
.setColor(interaction.member ? interaction.member.displayHexColor : 'NAVY')
|
2022-06-20 07:57:32 +02:00
|
|
|
.setAuthor({ name: `Downloaded by ${interaction.user.tag}`, iconURL: interaction.member.avatarURL(), url: url })
|
|
|
|
.setFooter({ text: `You can get the original video by clicking on the "Downloaded by ${interaction.user.tag}" message!` });
|
2022-06-16 09:18:39 +02:00
|
|
|
|
|
|
|
if (interaction.customId === 'downloadQuality') {
|
|
|
|
format = interaction.values[0];
|
2022-06-16 14:36:38 +02:00
|
|
|
if (interaction.values[1]) format += '+' + interaction.values[1];
|
2022-06-16 09:18:39 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 01:25:05 +02:00
|
|
|
utils.downloadVideo(url, interaction.id, format)
|
2022-06-16 09:18:39 +02:00
|
|
|
.then(async () => {
|
|
|
|
const file = fs.readdirSync(os.tmpdir()).filter(fn => fn.startsWith(interaction.id));
|
|
|
|
const output = `${os.tmpdir()}/${file}`;
|
|
|
|
|
|
|
|
const fileStat = fs.statSync(output);
|
|
|
|
const fileSize = fileStat.size / 1000000.0;
|
|
|
|
|
|
|
|
if (fileSize > 100) {
|
|
|
|
await interaction.deleteReply();
|
|
|
|
await interaction.followUp('Uh oh! The video you tried to download is too big!', { ephemeral: true });
|
|
|
|
}
|
|
|
|
else if (fileSize > 8) {
|
2022-06-17 01:25:05 +02:00
|
|
|
const fileURL = await utils.upload(output)
|
2022-06-16 09:18:39 +02:00
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
await interaction.editReply({ content: 'File was bigger than 8 mb. It has been uploaded to an external site.', embeds: [Embed], ephemeral: false });
|
|
|
|
await interaction.followUp({ content: fileURL, ephemeral: false });
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
await interaction.editReply({ embeds: [Embed], files: [output], ephemeral: false });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(async err => {
|
|
|
|
console.error(err);
|
|
|
|
await interaction.deleteReply();
|
|
|
|
await interaction.followUp({ content: 'Uh oh! An error has occured!', ephemeral: true });
|
|
|
|
});
|
2022-06-16 10:25:45 +02:00
|
|
|
return;
|
2022-06-16 09:18:39 +02:00
|
|
|
}
|