From fb027bc7a6ec7e966eab7b69db711786184de7b5 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sat, 17 Aug 2024 16:52:44 -0400 Subject: [PATCH] Update play.js --- commands/play.js | 80 ++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/commands/play.js b/commands/play.js index debf58f..58e594f 100644 --- a/commands/play.js +++ b/commands/play.js @@ -45,8 +45,8 @@ module.exports = { message.channel.send({ embeds: [embed] }); - addToQueue(message.guild.id, tempFilePath, title); - playNextInQueue(message.guild.id, voiceChannel); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel); + playNextInQueue(message.guild.id); return; } else { return message.reply('Only MP3 files are supported for uploads.'); @@ -54,38 +54,12 @@ module.exports = { } if (isValidURL(searchQuery)) { - if (searchQuery.includes('cdn.discordapp.com')) { - title = path.basename(searchQuery.split('?')[0]); - tempFilePath = path.join(__dirname, '../utils/tmp', title); - - console.log(`Discord CDN link received: ${searchQuery}`); - console.log(`Downloading to: ${tempFilePath}`); - - const response = await fetch(searchQuery); - if (!response.ok) throw new Error('Failed to download MP3 file.'); - const buffer = await response.buffer(); - fs.writeFileSync(tempFilePath, buffer); - - console.log(`Downloaded and saved: ${tempFilePath}`); - - const embed = new EmbedBuilder() - .setColor('#0099ff') - .setTitle('Now Playing') - .setDescription(`**${title}**`) - .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) - .setTimestamp(); - - message.channel.send({ embeds: [embed] }); - - addToQueue(message.guild.id, tempFilePath, title); - playNextInQueue(message.guild.id, voiceChannel); - return; - } else if (searchQuery.endsWith('.mp3')) { + if (searchQuery.endsWith('.mp3')) { title = path.basename(searchQuery.split('?')[0]); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`); - console.log(`Direct MP3 link received: ${searchQuery}`); - console.log(`Downloading to: ${tempFilePath}`); + console.log(`MP3 link received: ${searchQuery}`); + console.log(`Downloading MP3 to: ${tempFilePath}`); const response = await fetch(searchQuery); if (!response.ok) throw new Error('Failed to download MP3 file.'); @@ -103,14 +77,38 @@ module.exports = { message.channel.send({ embeds: [embed] }); - addToQueue(message.guild.id, tempFilePath, title); - playNextInQueue(message.guild.id, voiceChannel); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel); + playNextInQueue(message.guild.id); + return; + } else if (searchQuery.includes("cdn.discordapp.com")) { + title = path.basename(searchQuery.split('?')[0]); + tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`); + + console.log(`Discord MP3 link received: ${searchQuery}`); + console.log(`Downloading MP3 from Discord to: ${tempFilePath}`); + + const response = await fetch(searchQuery); + if (!response.ok) throw new Error('Failed to download MP3 file from Discord.'); + const buffer = await response.buffer(); + fs.writeFileSync(tempFilePath, buffer); + + console.log(`Downloaded and saved: ${tempFilePath}`); + + const embed = new EmbedBuilder() + .setColor('#0099ff') + .setTitle('Now Playing') + .setDescription(`**${title}**`) + .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) + .setTimestamp(); + + message.channel.send({ embeds: [embed] }); + + addToQueue(message.guild.id, tempFilePath, title, voiceChannel); + playNextInQueue(message.guild.id); return; } else { tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); - console.log(`YouTube link received: ${searchQuery}`); - exec(`yt-dlp --print title ${searchQuery}`, (error, stdout, stderr) => { if (error) { console.error(`Error getting title: ${error}`); @@ -128,8 +126,6 @@ module.exports = { return; } - console.log(`Downloaded and saved: ${tempFilePath}`); - const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Now Playing') @@ -139,8 +135,8 @@ module.exports = { message.channel.send({ embeds: [embed] }); - addToQueue(message.guild.id, tempFilePath, title); - playNextInQueue(message.guild.id, voiceChannel); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel); + playNextInQueue(message.guild.id); }); }); } @@ -166,8 +162,6 @@ module.exports = { return; } - console.log(`Downloaded and saved: ${tempFilePath}`); - const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Now Playing') @@ -177,8 +171,8 @@ module.exports = { message.channel.send({ embeds: [embed] }); - addToQueue(message.guild.id, tempFilePath, title); - playNextInQueue(message.guild.id, voiceChannel); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel); + playNextInQueue(message.guild.id); }); }); }