diff --git a/commands/play.js b/commands/play.js index 7dc0cc2..07db427 100644 --- a/commands/play.js +++ b/commands/play.js @@ -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; } diff --git a/package.json b/package.json index 9a5f912..a90e27f 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/utils/yt-dlp.js b/utils/yt-dlp.js index ce9ae0c..f70ece9 100644 --- a/utils/yt-dlp.js +++ b/utils/yt-dlp.js @@ -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')); diff --git a/yt-dlp b/yt-dlp new file mode 100755 index 0000000..509eb7e Binary files /dev/null and b/yt-dlp differ