Compare commits

...

5 Commits

Author SHA1 Message Date
a442fac4fd Merge branch 'yt-dlp-local-binary' 2025-05-31 19:12:28 -04:00
5aaa8d9817 Improvements 2025-05-31 19:10:39 -04:00
628ef7f47f test 2024-09-08 17:00:25 -04:00
c4768e7fb4 Update queue.js 2024-09-08 12:46:39 -04:00
56178c08cd Playlist support 2024-09-08 12:35:31 -04:00
5 changed files with 92 additions and 20 deletions

@ -8,6 +8,9 @@ const { exec, spawn, execSync } = require('child_process');
const MAX_RETRIES = 3;
const RETRY_DELAY = 1000;
const lastRequestTime = new Map();
const RATE_LIMIT_MS = 3000;
function spawnFFmpegProcess(args, callback, retries = 0) {
const ffmpegProcess = spawn('ffmpeg', args);
@ -53,6 +56,17 @@ module.exports = {
return message.reply('You need to be in a voice channel to play music!');
}
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;
@ -245,8 +259,9 @@ module.exports = {
} else {
console.log(`YouTube link received: ${searchQuery}`);
videoUrl = searchQuery;
const ytDlpPath = path.join(__dirname, '../yt-dlp');
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print-json --skip-download ${searchQuery}`, async (error, stdout, stderr) => {
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) {
console.error(`Error getting video info: ${error}`);
message.reply('Failed to retrieve video info.');
@ -264,10 +279,17 @@ module.exports = {
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(`"${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) {
console.error(`Error downloading file: ${error}`);
message.reply('Failed to download audio file.');
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;
}
@ -299,25 +321,56 @@ module.exports = {
loadingMessage = await message.channel.send(`Searching for: **${searchQuery}**...`);
console.log(`Performing YouTube search: ${searchQuery}`);
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --dump-single-json "ytsearch:${searchQuery}"`, (error, stdout, stderr) => {
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) {
console.error(`Error searching: ${error}`);
console.error(`stderr: ${stderr}`);
message.reply('Failed to search for video.');
return;
}
const info = JSON.parse(stdout);
const url = info.entries[0].webpage_url;
title = info.entries[0].title;
thumbnailUrl = info.entries[0].thumbnail;
let url;
try {
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;
}
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(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${url}`, async (error, stdout, stderr) => {
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}`);
message.reply('Failed to download audio file.');
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;
}

@ -9,7 +9,8 @@ module.exports = {
const queue = getQueue(message.guild.id);
const currentTrack = getCurrentTrack(message.guild.id);
const pageSize = 8;
const pageSize = 10;
const maxLength = 1024;
const embed = new EmbedBuilder()
.setColor('#0099ff')
@ -27,21 +28,31 @@ module.exports = {
}
if (queue.length > 0) {
const queueDisplay = queue.slice(0, pageSize).map((track, index) => {
let queueDisplay = '';
let songCount = 0;
for (let i = 0; i < Math.min(queue.length, pageSize); i++) {
const track = queue[i];
const trackDisplay = track.url
? `**${index + 1}.** [**${track.title}**](${track.url})\nRequested by: ${track.requester}`
: `**${index + 1}.** **${track.title}**\nRequested by: ${track.requester}`;
return trackDisplay;
}).join('\n\n');
? `**${i + 1}.** [**${track.title}**](${track.url})\nRequested by: ${track.requester}`
: `**${i + 1}.** **${track.title}**\nRequested by: ${track.requester}`;
if ((queueDisplay + trackDisplay).length > maxLength) {
break;
}
queueDisplay += trackDisplay + '\n\n';
songCount++;
}
embed.addFields({
name: 'Up next',
value: queueDisplay,
value: queueDisplay.trim(),
inline: false
});
if (queue.length > pageSize) {
embed.setFooter({ text: `And ${queue.length - pageSize} more...` });
if (queue.length > songCount) {
embed.setFooter({ text: `And ${queue.length - songCount} more...` });
}
} else if (!currentTrack) {
embed.setDescription('The queue is empty!');

@ -4,6 +4,7 @@
"discord.js": "^14.15.3",
"libsodium-wrappers": "^0.7.15",
"node-fetch": "^3.3.2",
"p-limit": "^6.1.0",
"uuid": "^10.0.0"
}
}

@ -4,6 +4,7 @@ const { v4: uuidv4 } = require('uuid');
async function downloadVideo(searchQuery) {
const tempFilePath = path.join(__dirname, 'tmp', `${uuidv4()}.mp3`);
const ytDlpPath = path.join(__dirname, '../yt-dlp');
const ytDlpArgs = [
'--cookies', path.join(__dirname, '../cookies.txt'),
@ -13,7 +14,13 @@ async function downloadVideo(searchQuery) {
];
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) {
console.error('yt-dlp error:', stderr);
return reject(new Error('Failed to download video with yt-dlp'));

BIN
yt-dlp Executable file

Binary file not shown.