Merge branch 'yt-dlp-local-binary'
This commit is contained in:
265
commands/play.js
265
commands/play.js
@ -3,12 +3,15 @@ const { v4: uuidv4 } = require('uuid');
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { EmbedBuilder } = require('discord.js');
|
const { EmbedBuilder } = require('discord.js');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const { exec, execSync } = require('child_process');
|
const { exec, spawn, execSync } = require('child_process');
|
||||||
|
|
||||||
const MAX_RETRIES = 3;
|
const MAX_RETRIES = 3;
|
||||||
const RETRY_DELAY = 1000;
|
const RETRY_DELAY = 1000;
|
||||||
|
|
||||||
async function spawnFFmpegProcess(args, callback, retries = 0) {
|
const lastRequestTime = new Map();
|
||||||
|
const RATE_LIMIT_MS = 3000;
|
||||||
|
|
||||||
|
function spawnFFmpegProcess(args, callback, retries = 0) {
|
||||||
const ffmpegProcess = spawn('ffmpeg', args);
|
const ffmpegProcess = spawn('ffmpeg', args);
|
||||||
|
|
||||||
ffmpegProcess.on('close', (code) => {
|
ffmpegProcess.on('close', (code) => {
|
||||||
@ -42,43 +45,62 @@ module.exports = {
|
|||||||
aliases: ['p'],
|
aliases: ['p'],
|
||||||
async execute(message, args) {
|
async execute(message, args) {
|
||||||
const fetch = await import('node-fetch').then(module => module.default);
|
const fetch = await import('node-fetch').then(module => module.default);
|
||||||
const limit = (await import('p-limit')).default(3);
|
|
||||||
const searchQuery = args.join(' ');
|
const searchQuery = args.join(' ');
|
||||||
const voiceChannel = message.member.voice.channel;
|
const voiceChannel = message.member.voice.channel;
|
||||||
|
|
||||||
|
console.log(`Received command: play ${searchQuery}`);
|
||||||
|
console.log(`Voice channel: ${voiceChannel ? voiceChannel.name : 'None'}`);
|
||||||
|
|
||||||
if (!voiceChannel) {
|
if (!voiceChannel) {
|
||||||
|
console.error('User is not in a voice channel.');
|
||||||
return message.reply('You need to be in a voice channel to play music!');
|
return message.reply('You need to be in a voice channel to play music!');
|
||||||
}
|
}
|
||||||
|
|
||||||
const isPlaylist = searchQuery.includes("list=");
|
const userId = message.author.id;
|
||||||
|
const now = Date.now();
|
||||||
|
const lastRequest = lastRequestTime.get(userId);
|
||||||
|
|
||||||
|
if (lastRequest && (now - lastRequest) < RATE_LIMIT_MS) {
|
||||||
|
const remainingTime = Math.ceil((RATE_LIMIT_MS - (now - lastRequest)) / 1000);
|
||||||
|
return message.reply(`Please wait ${remainingTime} more seconds before making another request.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
lastRequestTime.set(userId, now);
|
||||||
|
|
||||||
|
let title, tempFilePath, videoUrl = null, thumbnailUrl = null;
|
||||||
let loadingMessage;
|
let loadingMessage;
|
||||||
|
|
||||||
const getDuration = (filePath) => {
|
|
||||||
try {
|
|
||||||
const output = execSync(`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${filePath}"`).toString().trim();
|
|
||||||
const durationSeconds = parseFloat(output);
|
|
||||||
const minutes = Math.floor(durationSeconds / 60);
|
|
||||||
const seconds = Math.floor(durationSeconds % 60);
|
|
||||||
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error getting track duration:', error);
|
|
||||||
return 'Unknown';
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
const getDuration = (filePath) => {
|
||||||
|
try {
|
||||||
|
const output = execSync(`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${filePath}"`).toString().trim();
|
||||||
|
const durationSeconds = parseFloat(output);
|
||||||
|
const minutes = Math.floor(durationSeconds / 60);
|
||||||
|
const seconds = Math.floor(durationSeconds % 60);
|
||||||
|
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error getting track duration:', error);
|
||||||
|
return 'Unknown';
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if (message.attachments.size > 0) {
|
if (message.attachments.size > 0) {
|
||||||
const attachment = message.attachments.first();
|
const attachment = message.attachments.first();
|
||||||
const attachmentName = attachment.name.toLowerCase();
|
const attachmentName = attachment.name.toLowerCase();
|
||||||
|
|
||||||
if (attachmentName.endsWith('.mp3')) {
|
if (attachmentName.endsWith('.mp3')) {
|
||||||
const title = attachment.name;
|
title = attachment.name;
|
||||||
const tempFilePath = path.join(__dirname, '../utils/tmp', attachment.name);
|
tempFilePath = path.join(__dirname, '../utils/tmp', attachment.name);
|
||||||
|
|
||||||
|
console.log(`Attachment received: ${title}`);
|
||||||
|
console.log(`Downloading attachment to: ${tempFilePath}`);
|
||||||
|
|
||||||
const response = await fetch(attachment.url);
|
const response = await fetch(attachment.url);
|
||||||
const buffer = await response.buffer();
|
const buffer = await response.buffer();
|
||||||
fs.writeFileSync(tempFilePath, buffer);
|
fs.writeFileSync(tempFilePath, buffer);
|
||||||
|
|
||||||
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
|
|
||||||
const duration = getDuration(tempFilePath);
|
const duration = getDuration(tempFilePath);
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
@ -90,13 +112,17 @@ module.exports = {
|
|||||||
|
|
||||||
message.channel.send({ embeds: [embed] });
|
message.channel.send({ embeds: [embed] });
|
||||||
|
|
||||||
|
console.log('Adding to queue and attempting to play.');
|
||||||
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
||||||
playNextInQueue(message.guild.id);
|
playNextInQueue(message.guild.id);
|
||||||
return;
|
return;
|
||||||
} else if (attachmentName.endsWith('.mp4') || attachmentName.endsWith('.webm') || attachmentName.endsWith('.mov')) {
|
} else if (attachmentName.endsWith('.mp4') || attachmentName.endsWith('.webm') || attachmentName.endsWith('.mov')) {
|
||||||
const title = attachment.name;
|
title = attachment.name;
|
||||||
const convertedFileName = `${uuidv4()}.mp3`;
|
const convertedFileName = `${uuidv4()}.mp3`;
|
||||||
const tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
|
tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
|
||||||
|
|
||||||
|
console.log(`Attachment received: ${title}`);
|
||||||
|
console.log(`Converting and downloading attachment to: ${tempFilePath}`);
|
||||||
|
|
||||||
const response = await fetch(attachment.url);
|
const response = await fetch(attachment.url);
|
||||||
const buffer = await response.buffer();
|
const buffer = await response.buffer();
|
||||||
@ -105,10 +131,12 @@ module.exports = {
|
|||||||
|
|
||||||
spawnFFmpegProcess(['-i', tempVideoPath, '-f', 'mp3', '-ab', '192000', '-vn', tempFilePath], (err) => {
|
spawnFFmpegProcess(['-i', tempVideoPath, '-f', 'mp3', '-ab', '192000', '-vn', tempFilePath], (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
console.error('Error converting file:', err);
|
||||||
message.reply('Failed to convert the video file.');
|
message.reply('Failed to convert the video file.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`Converted and saved: ${tempFilePath}`);
|
||||||
fs.unlinkSync(tempVideoPath);
|
fs.unlinkSync(tempVideoPath);
|
||||||
|
|
||||||
const duration = getDuration(tempFilePath);
|
const duration = getDuration(tempFilePath);
|
||||||
@ -122,26 +150,33 @@ module.exports = {
|
|||||||
|
|
||||||
message.channel.send({ embeds: [embed] });
|
message.channel.send({ embeds: [embed] });
|
||||||
|
|
||||||
|
console.log('Adding to queue and attempting to play.');
|
||||||
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
||||||
playNextInQueue(message.guild.id);
|
playNextInQueue(message.guild.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
|
console.error('Attachment is not a supported media file.');
|
||||||
return message.reply('Only MP3, MP4, WEBM, and MOV files are supported for uploads.');
|
return message.reply('Only MP3, MP4, WEBM, and MOV files are supported for uploads.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isValidURL(searchQuery)) {
|
if (isValidURL(searchQuery)) {
|
||||||
if (searchQuery.endsWith('.mp3')) {
|
if (searchQuery.endsWith('.mp3')) {
|
||||||
const title = path.basename(searchQuery.split('?')[0]);
|
title = path.basename(searchQuery.split('?')[0]);
|
||||||
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
|
||||||
|
|
||||||
|
console.log(`MP3 link received: ${searchQuery}`);
|
||||||
|
console.log(`Downloading MP3 to: ${tempFilePath}`);
|
||||||
|
|
||||||
const response = await fetch(searchQuery);
|
const response = await fetch(searchQuery);
|
||||||
if (!response.ok) throw new Error('Failed to download MP3 file.');
|
if (!response.ok) throw new Error('Failed to download MP3 file.');
|
||||||
const buffer = await response.buffer();
|
const buffer = await response.buffer();
|
||||||
fs.writeFileSync(tempFilePath, buffer);
|
fs.writeFileSync(tempFilePath, buffer);
|
||||||
|
|
||||||
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
|
|
||||||
const duration = getDuration(tempFilePath);
|
const duration = getDuration(tempFilePath);
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
@ -153,15 +188,19 @@ module.exports = {
|
|||||||
|
|
||||||
message.channel.send({ embeds: [embed] });
|
message.channel.send({ embeds: [embed] });
|
||||||
|
|
||||||
|
console.log('Adding to queue and attempting to play.');
|
||||||
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
||||||
playNextInQueue(message.guild.id);
|
playNextInQueue(message.guild.id);
|
||||||
return;
|
return;
|
||||||
} else if (searchQuery.includes("cdn.discordapp.com")) {
|
} else if (searchQuery.includes("cdn.discordapp.com")) {
|
||||||
const title = path.basename(searchQuery.split('?')[0]);
|
title = path.basename(searchQuery.split('?')[0]);
|
||||||
const isVideo = searchQuery.endsWith('.mp4') || searchQuery.endsWith('.webm') || searchQuery.endsWith('.mov');
|
const isVideo = searchQuery.endsWith('.mp4') || searchQuery.endsWith('.webm') || searchQuery.endsWith('.mov');
|
||||||
const fileExtension = isVideo ? 'mp3' : 'original';
|
const fileExtension = isVideo ? 'mp3' : 'original';
|
||||||
const convertedFileName = `${uuidv4()}.${fileExtension}`;
|
const convertedFileName = `${uuidv4()}.${fileExtension}`;
|
||||||
const tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
|
tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
|
||||||
|
|
||||||
|
console.log(`Discord media link received: ${searchQuery}`);
|
||||||
|
console.log(`Downloading media from Discord to: ${tempFilePath}`);
|
||||||
|
|
||||||
const response = await fetch(searchQuery);
|
const response = await fetch(searchQuery);
|
||||||
if (!response.ok) throw new Error('Failed to download media file from Discord.');
|
if (!response.ok) throw new Error('Failed to download media file from Discord.');
|
||||||
@ -173,10 +212,12 @@ module.exports = {
|
|||||||
|
|
||||||
spawnFFmpegProcess(['-i', tempVideoPath, '-f', 'mp3', '-ab', '192000', '-vn', tempFilePath], (err) => {
|
spawnFFmpegProcess(['-i', tempVideoPath, '-f', 'mp3', '-ab', '192000', '-vn', tempFilePath], (err) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
console.error('Error converting file:', err);
|
||||||
message.reply('Failed to convert the video file.');
|
message.reply('Failed to convert the video file.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`Converted and saved: ${tempFilePath}`);
|
||||||
fs.unlinkSync(tempVideoPath);
|
fs.unlinkSync(tempVideoPath);
|
||||||
|
|
||||||
const duration = getDuration(tempFilePath);
|
const duration = getDuration(tempFilePath);
|
||||||
@ -190,12 +231,14 @@ module.exports = {
|
|||||||
|
|
||||||
message.channel.send({ embeds: [embed] });
|
message.channel.send({ embeds: [embed] });
|
||||||
|
|
||||||
|
console.log('Adding to queue and attempting to play.');
|
||||||
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
||||||
playNextInQueue(message.guild.id);
|
playNextInQueue(message.guild.id);
|
||||||
});
|
});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
fs.writeFileSync(tempFilePath, buffer);
|
fs.writeFileSync(tempFilePath, buffer);
|
||||||
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
|
|
||||||
const duration = getDuration(tempFilePath);
|
const duration = getDuration(tempFilePath);
|
||||||
|
|
||||||
@ -208,54 +251,50 @@ module.exports = {
|
|||||||
|
|
||||||
message.channel.send({ embeds: [embed] });
|
message.channel.send({ embeds: [embed] });
|
||||||
|
|
||||||
|
console.log('Adding to queue and attempting to play.');
|
||||||
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
|
||||||
playNextInQueue(message.guild.id);
|
playNextInQueue(message.guild.id);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
} else if (isPlaylist) {
|
|
||||||
loadingMessage = await message.channel.send(`Loading playlist...`);
|
|
||||||
|
|
||||||
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --flat-playlist --print-json "${searchQuery}"`, async (error, stdout) => {
|
|
||||||
if (error) {
|
|
||||||
message.reply('Failed to retrieve playlist info.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const playlist = stdout.split('\n').filter(Boolean).map(line => JSON.parse(line));
|
|
||||||
if (loadingMessage) await loadingMessage.delete();
|
|
||||||
|
|
||||||
message.channel.send(`Adding ${playlist.length} songs to the queue...`);
|
|
||||||
|
|
||||||
// Process the playlist with concurrency control using p-limit
|
|
||||||
const addToQueuePromises = playlist.map((video) => {
|
|
||||||
const videoUrl = `https://www.youtube.com/watch?v=${video.id}`;
|
|
||||||
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 {
|
} else {
|
||||||
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print-json --skip-download "${searchQuery}"`, async (error, stdout) => {
|
console.log(`YouTube link received: ${searchQuery}`);
|
||||||
|
videoUrl = searchQuery;
|
||||||
|
const ytDlpPath = path.join(__dirname, '../yt-dlp');
|
||||||
|
|
||||||
|
exec(`"${ytDlpPath}" --cookies ${path.join(__dirname, '../cookies.txt')} --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" --sleep-interval 1 --max-sleep-interval 3 --print-json --skip-download ${searchQuery}`, async (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
console.error(`Error getting video info: ${error}`);
|
||||||
message.reply('Failed to retrieve video info.');
|
message.reply('Failed to retrieve video info.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const info = JSON.parse(stdout);
|
const info = JSON.parse(stdout);
|
||||||
const title = info.title || "Unknown Title";
|
title = info.title || "Unknown Title";
|
||||||
const thumbnailUrl = info.thumbnail || null;
|
thumbnailUrl = info.thumbnail || null;
|
||||||
|
|
||||||
|
console.log(`Retrieved title: ${title}`);
|
||||||
|
console.log(`Thumbnail URL: ${thumbnailUrl}`);
|
||||||
|
|
||||||
loadingMessage = await message.channel.send(`**Loading...** ${title}`);
|
loadingMessage = await message.channel.send(`**Loading...** ${title}`);
|
||||||
|
|
||||||
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
||||||
|
|
||||||
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" "${searchQuery}"`, async (error) => {
|
exec(`"${ytDlpPath}" --cookies ${path.join(__dirname, '../cookies.txt')} --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" --sleep-interval 1 --max-sleep-interval 3 --format bestaudio --output "${tempFilePath}" ${searchQuery}`, async (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
message.reply('Failed to download audio file.');
|
console.error(`Error downloading file: ${error}`);
|
||||||
|
console.error(`stderr: ${stderr}`);
|
||||||
|
|
||||||
|
if (stderr.includes('429') || stderr.includes('Too Many Requests') || stderr.includes('Sign in to confirm')) {
|
||||||
|
message.reply('YouTube is currently rate limiting requests. Please try again in a few minutes, or use a direct MP3 link instead.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.reply('Failed to download audio file. This might be due to YouTube restrictions.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
|
|
||||||
const duration = getDuration(tempFilePath);
|
const duration = getDuration(tempFilePath);
|
||||||
|
|
||||||
if (loadingMessage) {
|
if (loadingMessage) {
|
||||||
@ -272,24 +311,91 @@ module.exports = {
|
|||||||
|
|
||||||
message.channel.send({ embeds: [embed] });
|
message.channel.send({ embeds: [embed] });
|
||||||
|
|
||||||
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, searchQuery, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
|
console.log('Adding to queue and attempting to play.');
|
||||||
|
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
|
||||||
playNextInQueue(message.guild.id);
|
playNextInQueue(message.guild.id);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
loadingMessage = await message.channel.send(`Searching for: **${searchQuery}**...`);
|
loadingMessage = await message.channel.send(`Searching for: **${searchQuery}**...`);
|
||||||
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --dump-single-json "ytsearch:${searchQuery}"`, async (error, stdout) => {
|
|
||||||
|
console.log(`Performing YouTube search: ${searchQuery}`);
|
||||||
|
const ytDlpPath = path.join(__dirname, '../yt-dlp');
|
||||||
|
exec(`"${ytDlpPath}" --cookies ${path.join(__dirname, '../cookies.txt')} --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" --sleep-interval 1 --max-sleep-interval 3 --flat-playlist --print-json "ytsearch1:${searchQuery}"`, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
console.error(`Error searching: ${error}`);
|
||||||
|
console.error(`stderr: ${stderr}`);
|
||||||
message.reply('Failed to search for video.');
|
message.reply('Failed to search for video.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const info = JSON.parse(stdout);
|
let url;
|
||||||
const videoUrl = info.entries[0].webpage_url;
|
try {
|
||||||
await addVideoToQueue(videoUrl, message, voiceChannel, true);
|
const lines = stdout.trim().split('\n').filter(line => line.trim());
|
||||||
|
if (lines.length === 0) {
|
||||||
|
console.error('No search results found');
|
||||||
|
message.reply('No videos found for your search.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (loadingMessage) await loadingMessage.delete();
|
const info = JSON.parse(lines[0]);
|
||||||
|
|
||||||
|
if (!info.url && !info.webpage_url) {
|
||||||
|
console.error('No valid URL found in search results');
|
||||||
|
message.reply('No valid videos found for your search.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
url = info.url || info.webpage_url || `https://www.youtube.com/watch?v=${info.id}`;
|
||||||
|
title = info.title;
|
||||||
|
thumbnailUrl = info.thumbnail || (info.thumbnails && info.thumbnails.length > 0 ? info.thumbnails[0].url : null);
|
||||||
|
} catch (parseError) {
|
||||||
|
console.error('Error parsing JSON:', parseError);
|
||||||
|
console.error('Raw stdout:', stdout);
|
||||||
|
message.reply('Failed to parse search results.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
||||||
|
console.log(`Downloading file to: ${tempFilePath}`);
|
||||||
|
|
||||||
|
exec(`"${ytDlpPath}" --cookies ${path.join(__dirname, '../cookies.txt')} --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" --sleep-interval 1 --max-sleep-interval 3 --format bestaudio --output "${tempFilePath}" ${url}`, async (error, stdout, stderr) => {
|
||||||
|
if (error) {
|
||||||
|
console.error(`Error downloading file: ${error}`);
|
||||||
|
console.error(`stderr: ${stderr}`);
|
||||||
|
|
||||||
|
if (stderr.includes('429') || stderr.includes('Too Many Requests') || stderr.includes('Sign in to confirm')) {
|
||||||
|
message.reply('YouTube is currently rate limiting requests. Please try again in a few minutes, or use a direct MP3 link instead.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
message.reply('Failed to download audio file. This might be due to YouTube restrictions.');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
|
|
||||||
|
const duration = getDuration(tempFilePath);
|
||||||
|
|
||||||
|
if (loadingMessage) {
|
||||||
|
await loadingMessage.delete();
|
||||||
|
}
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setColor('#0099ff')
|
||||||
|
.setTitle('Added To Queue')
|
||||||
|
.setDescription(`**${title}** (${duration})`)
|
||||||
|
.setThumbnail(thumbnailUrl)
|
||||||
|
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
|
||||||
|
.setTimestamp();
|
||||||
|
|
||||||
|
message.channel.send({ embeds: [embed] });
|
||||||
|
|
||||||
|
console.log('Adding to queue and attempting to play.');
|
||||||
|
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
|
||||||
|
playNextInQueue(message.guild.id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,43 +407,6 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
async function addVideoToQueue(videoUrl, message, voiceChannel, sendEmbed = true) {
|
|
||||||
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
|
||||||
|
|
||||||
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print-json --skip-download "${videoUrl}"`, async (error, stdout) => {
|
|
||||||
if (error) {
|
|
||||||
message.reply('Failed to retrieve video info.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const info = JSON.parse(stdout);
|
|
||||||
const title = info.title;
|
|
||||||
const thumbnailUrl = info.thumbnail;
|
|
||||||
|
|
||||||
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" "${videoUrl}"`, async (error) => {
|
|
||||||
if (error) {
|
|
||||||
message.reply('Failed to download audio file.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sendEmbed) {
|
|
||||||
const embed = new EmbedBuilder()
|
|
||||||
.setColor('#0099ff')
|
|
||||||
.setTitle('Added To Queue')
|
|
||||||
.setDescription(`**${title}**`)
|
|
||||||
.setThumbnail(thumbnailUrl)
|
|
||||||
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
|
|
||||||
.setTimestamp();
|
|
||||||
|
|
||||||
message.channel.send({ embeds: [embed] });
|
|
||||||
}
|
|
||||||
|
|
||||||
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
|
|
||||||
playNextInQueue(message.guild.id);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function isValidURL(string) {
|
function isValidURL(string) {
|
||||||
try {
|
try {
|
||||||
new URL(string);
|
new URL(string);
|
||||||
@ -345,4 +414,4 @@ function isValidURL(string) {
|
|||||||
} catch (_) {
|
} catch (_) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,6 +4,7 @@
|
|||||||
"discord.js": "^14.15.3",
|
"discord.js": "^14.15.3",
|
||||||
"libsodium-wrappers": "^0.7.15",
|
"libsodium-wrappers": "^0.7.15",
|
||||||
"node-fetch": "^3.3.2",
|
"node-fetch": "^3.3.2",
|
||||||
|
"p-limit": "^6.1.0",
|
||||||
"uuid": "^10.0.0"
|
"uuid": "^10.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ const { v4: uuidv4 } = require('uuid');
|
|||||||
|
|
||||||
async function downloadVideo(searchQuery) {
|
async function downloadVideo(searchQuery) {
|
||||||
const tempFilePath = path.join(__dirname, 'tmp', `${uuidv4()}.mp3`);
|
const tempFilePath = path.join(__dirname, 'tmp', `${uuidv4()}.mp3`);
|
||||||
|
const ytDlpPath = path.join(__dirname, '../yt-dlp');
|
||||||
|
|
||||||
const ytDlpArgs = [
|
const ytDlpArgs = [
|
||||||
'--cookies', path.join(__dirname, '../cookies.txt'),
|
'--cookies', path.join(__dirname, '../cookies.txt'),
|
||||||
@ -13,7 +14,13 @@ async function downloadVideo(searchQuery) {
|
|||||||
];
|
];
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
exec(`yt-dlp ${ytDlpArgs.join(' ')}`, (error, stdout, stderr) => {
|
const fullArgs = [
|
||||||
|
'--user-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
||||||
|
'--sleep-interval', '1',
|
||||||
|
'--max-sleep-interval', '3',
|
||||||
|
...ytDlpArgs
|
||||||
|
];
|
||||||
|
exec(`"${ytDlpPath}" ${fullArgs.join(' ')}`, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error('yt-dlp error:', stderr);
|
console.error('yt-dlp error:', stderr);
|
||||||
return reject(new Error('Failed to download video with yt-dlp'));
|
return reject(new Error('Failed to download video with yt-dlp'));
|
||||||
|
BIN
yt-dlp
Executable file
BIN
yt-dlp
Executable file
Binary file not shown.
Reference in New Issue
Block a user