diff --git a/commands/play.js b/commands/play.js index 80bdff7..aa44cc8 100644 --- a/commands/play.js +++ b/commands/play.js @@ -21,7 +21,8 @@ module.exports = { return message.reply('You need to be in a voice channel to play music!'); } - let title, tempFilePath, videoUrl; + let title, tempFilePath, videoUrl = null; + let loadingMessage; try { if (message.attachments.size > 0) { @@ -50,7 +51,7 @@ module.exports = { message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL()); playNextInQueue(message.guild.id); return; } else { @@ -84,7 +85,7 @@ module.exports = { message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL()); playNextInQueue(message.guild.id); return; } else if (searchQuery.includes("cdn.discordapp.com")) { @@ -111,14 +112,17 @@ module.exports = { message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL()); playNextInQueue(message.guild.id); return; } else { + loadingMessage = await message.channel.send(`**Loading**...`); + tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); - videoUrl = searchQuery; console.log(`YouTube link received: ${searchQuery}`); + videoUrl = searchQuery; + exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title ${searchQuery}`, (error, stdout, stderr) => { if (error) { console.error(`Error getting title: ${error}`); @@ -129,7 +133,7 @@ module.exports = { title = stdout.trim() || "Unknown Title"; console.log(`Retrieved title: ${title}`); - exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${searchQuery}`, (error, stdout, stderr) => { + exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${searchQuery}`, async (error, stdout, stderr) => { if (error) { console.error(`Error downloading file: ${error}`); message.reply('Failed to download audio file.'); @@ -138,6 +142,10 @@ module.exports = { console.log(`Downloaded and saved: ${tempFilePath}`); + if (loadingMessage) { + await loadingMessage.delete(); + } + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Now Playing') @@ -154,6 +162,8 @@ module.exports = { }); } } else { + 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) => { if (error) { @@ -165,12 +175,11 @@ module.exports = { const info = JSON.parse(stdout); const url = info.entries[0].webpage_url; title = info.entries[0].title; - videoUrl = url; 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}`, (error, stdout, stderr) => { + exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${url}`, async (error, stdout, stderr) => { if (error) { console.error(`Error downloading file: ${error}`); message.reply('Failed to download audio file.'); @@ -179,6 +188,10 @@ module.exports = { console.log(`Downloaded and saved: ${tempFilePath}`); + if (loadingMessage) { + await loadingMessage.delete(); + } + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Now Playing') @@ -189,7 +202,7 @@ module.exports = { message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL()); playNextInQueue(message.guild.id); }); }); @@ -197,6 +210,7 @@ module.exports = { } catch (error) { console.error('Error:', error); + if (loadingMessage) await loadingMessage.delete(); message.reply('An error occurred while trying to play the music.'); } }, diff --git a/commands/queue.js b/commands/queue.js index b8f7fa3..9f80308 100644 --- a/commands/queue.js +++ b/commands/queue.js @@ -42,4 +42,4 @@ module.exports = { message.channel.send({ embeds: [embed] }); } -}; +}; \ No newline at end of file