diff --git a/commands/play.js b/commands/play.js index d52a224..9e2a4b2 100644 --- a/commands/play.js +++ b/commands/play.js @@ -1,5 +1,5 @@ const { addToQueue, playNextInQueue } = require('../utils/queueManager'); -const ytDlpExec = require('yt-dlp-exec'); +const { exec } = require('child_process'); const { v4: uuidv4 } = require('uuid'); const path = require('path'); const { EmbedBuilder } = require('discord.js'); @@ -48,13 +48,8 @@ module.exports = { } } - if (!searchQuery) { - return message.reply('Please provide a YouTube link, MP3 link, or a song name.'); - } - if (isValidURL(searchQuery)) { url = searchQuery; - if (url.endsWith('.mp3')) { title = path.basename(url); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); @@ -63,34 +58,22 @@ module.exports = { const buffer = await response.buffer(); fs.writeFileSync(tempFilePath, buffer); } else { - const info = await ytDlpExec(url, { dumpSingleJson: true }); - title = info.title; - tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); - await ytDlpExec(url, { - cookies: path.join(__dirname, '../cookies.txt'), - format: 'bestaudio', - output: tempFilePath, - quiet: true, - }); + title = await downloadWithYtDlp(url, tempFilePath); } } else { - const searchResult = await ytDlpExec(`ytsearch:${searchQuery}`, { - dumpSingleJson: true, - noPlaylist: true, - format: 'bestaudio/best', - quiet: true, - }); + const searchUrl = `ytsearch:${searchQuery}`; + const searchResult = await getYtDlpInfo(searchUrl); + + if (!searchResult || !searchResult.entries || searchResult.entries.length === 0) { + return message.reply('No results found for your search query.'); + } + url = searchResult.entries[0].webpage_url; title = searchResult.entries[0].title; tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); - await ytDlpExec(url, { - cookies: path.join(__dirname, '../cookies.txt'), - format: 'bestaudio', - output: tempFilePath, - quiet: true, - }); + await downloadWithYtDlp(url, tempFilePath); } const embed = new EmbedBuilder() @@ -107,7 +90,7 @@ module.exports = { } catch (error) { console.error('yt-dlp error:', error); - message.reply('Failed to retrieve or download video. Please try again.'); + message.reply('Failed to retrieve or download the video. Please try again.'); } }, }; @@ -119,4 +102,30 @@ function isValidURL(string) { } catch (_) { return false; } -} \ No newline at end of file +} + +function downloadWithYtDlp(url, output) { + return new Promise((resolve, reject) => { + const command = `yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output ${output} --quiet --verbose "${url}"`; + exec(command, (error, stdout, stderr) => { + if (error) { + reject(`yt-dlp error: ${stderr}`); + } else { + resolve(output); + } + }); + }); +} + +function getYtDlpInfo(url) { + return new Promise((resolve, reject) => { + const command = `yt-dlp --dump-single-json "${url}"`; + exec(command, (error, stdout, stderr) => { + if (error) { + reject(`yt-dlp info error: ${stderr}`); + } else { + resolve(JSON.parse(stdout)); + } + }); + }); +}