This commit is contained in:
Wizzard 2024-09-08 17:00:25 -04:00
parent c4768e7fb4
commit 628ef7f47f
1 changed files with 20 additions and 20 deletions

View File

@ -8,7 +8,7 @@ const { exec, execSync } = require('child_process');
const MAX_RETRIES = 3;
const RETRY_DELAY = 1000;
function spawnFFmpegProcess(args, callback, retries = 0) {
async function spawnFFmpegProcess(args, callback, retries = 0) {
const ffmpegProcess = spawn('ffmpeg', args);
ffmpegProcess.on('close', (code) => {
@ -42,6 +42,7 @@ module.exports = {
aliases: ['p'],
async execute(message, args) {
const fetch = await import('node-fetch').then(module => module.default);
const limit = (await import('p-limit')).default(3);
const searchQuery = args.join(' ');
const voiceChannel = message.member.voice.channel;
@ -71,8 +72,8 @@ module.exports = {
const attachmentName = attachment.name.toLowerCase();
if (attachmentName.endsWith('.mp3')) {
title = attachment.name;
tempFilePath = path.join(__dirname, '../utils/tmp', attachment.name);
const title = attachment.name;
const tempFilePath = path.join(__dirname, '../utils/tmp', attachment.name);
const response = await fetch(attachment.url);
const buffer = await response.buffer();
@ -93,9 +94,9 @@ module.exports = {
playNextInQueue(message.guild.id);
return;
} else if (attachmentName.endsWith('.mp4') || attachmentName.endsWith('.webm') || attachmentName.endsWith('.mov')) {
title = attachment.name;
const title = attachment.name;
const convertedFileName = `${uuidv4()}.mp3`;
tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
const tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
const response = await fetch(attachment.url);
const buffer = await response.buffer();
@ -133,8 +134,8 @@ module.exports = {
if (isValidURL(searchQuery)) {
if (searchQuery.endsWith('.mp3')) {
title = path.basename(searchQuery.split('?')[0]);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
const title = path.basename(searchQuery.split('?')[0]);
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
const response = await fetch(searchQuery);
if (!response.ok) throw new Error('Failed to download MP3 file.');
@ -156,11 +157,11 @@ module.exports = {
playNextInQueue(message.guild.id);
return;
} else if (searchQuery.includes("cdn.discordapp.com")) {
title = path.basename(searchQuery.split('?')[0]);
const title = path.basename(searchQuery.split('?')[0]);
const isVideo = searchQuery.endsWith('.mp4') || searchQuery.endsWith('.webm') || searchQuery.endsWith('.mov');
const fileExtension = isVideo ? 'mp3' : 'original';
const convertedFileName = `${uuidv4()}.${fileExtension}`;
tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
const tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
const response = await fetch(searchQuery);
if (!response.ok) throw new Error('Failed to download media file from Discord.');
@ -225,11 +226,13 @@ module.exports = {
message.channel.send(`Adding ${playlist.length} songs to the queue...`);
for (const video of playlist) {
// Process the playlist with concurrency control using p-limit
const addToQueuePromises = playlist.map((video) => {
const videoUrl = `https://www.youtube.com/watch?v=${video.id}`;
await addVideoToQueue(videoUrl, message, voiceChannel, false);
}
return limit(() => addVideoToQueue(videoUrl, message, voiceChannel, false));
});
await Promise.all(addToQueuePromises); // Wait for all promises to complete with concurrency limit
playNextInQueue(message.guild.id);
});
} else {
@ -240,17 +243,14 @@ module.exports = {
}
const info = JSON.parse(stdout);
title = info.title || "Unknown Title";
thumbnailUrl = info.thumbnail || null;
console.log(`Retrieved title: ${title}`);
console.log(`Thumbnail URL: ${thumbnailUrl}`);
const title = info.title || "Unknown Title";
const thumbnailUrl = info.thumbnail || null;
loadingMessage = await message.channel.send(`**Loading...** ${title}`);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" "${searchQuery}"`, async (error, stdout, stderr) => {
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" "${searchQuery}"`, async (error) => {
if (error) {
message.reply('Failed to download audio file.');
return;
@ -345,4 +345,4 @@ function isValidURL(string) {
} catch (_) {
return false;
}
}
}