From f4788940020cd83331c34206e662e90ca5e87dde Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sat, 17 Aug 2024 16:37:26 -0400 Subject: [PATCH] Update play.js --- commands/play.js | 122 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 91 insertions(+), 31 deletions(-) diff --git a/commands/play.js b/commands/play.js index f3adc61..debf58f 100644 --- a/commands/play.js +++ b/commands/play.js @@ -3,6 +3,7 @@ const { v4: uuidv4 } = require('uuid'); const path = require('path'); const { EmbedBuilder } = require('discord.js'); const fs = require('fs'); +const fetch = require('node-fetch'); const { exec } = require('child_process'); module.exports = { @@ -16,7 +17,7 @@ module.exports = { return message.reply('You need to be in a voice channel to play music!'); } - let url, title, tempFilePath; + let title, tempFilePath; try { if (message.attachments.size > 0) { @@ -24,12 +25,17 @@ module.exports = { if (attachment.name.endsWith('.mp3')) { title = attachment.name; - tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); + tempFilePath = path.join(__dirname, '../utils/tmp', attachment.name); + + console.log(`Attachment received: ${title}`); + console.log(`Downloading attachment to: ${tempFilePath}`); const response = await fetch(attachment.url); const buffer = await response.buffer(); fs.writeFileSync(tempFilePath, buffer); + console.log(`Downloaded and saved: ${tempFilePath}`); + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Now Playing') @@ -47,45 +53,97 @@ module.exports = { } } - if (!searchQuery) { - return message.reply('Please provide a YouTube link, MP3 link, or a song name.'); - } - if (isValidURL(searchQuery)) { - url = searchQuery; - tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); + if (searchQuery.includes('cdn.discordapp.com')) { + title = path.basename(searchQuery.split('?')[0]); + tempFilePath = path.join(__dirname, '../utils/tmp', title); - exec(`yt-dlp --print title ${url}`, (error, stdout, stderr) => { - if (error) { - console.error(`Error getting title: ${error}`); - message.reply('Failed to retrieve video title.'); - return; - } + console.log(`Discord CDN link received: ${searchQuery}`); + console.log(`Downloading to: ${tempFilePath}`); - title = stdout.trim(); - console.log(`Retrieved title: ${title}`); + 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); - // Now download the file - exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${url}`, (error, stdout, stderr) => { + 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')) { + 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}`); + + 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 { + 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 downloading file: ${error}`); - message.reply('Failed to download audio file.'); + console.error(`Error getting title: ${error}`); + message.reply('Failed to retrieve video title.'); return; } - const embed = new EmbedBuilder() - .setColor('#0099ff') - .setTitle('Now Playing') - .setDescription(`**${title}**`) - .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) - .setTimestamp(); + title = stdout.trim() || "Unknown Title"; + console.log(`Retrieved title: ${title}`); - message.channel.send({ embeds: [embed] }); + exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${searchQuery}`, (error, stdout, stderr) => { + if (error) { + console.error(`Error downloading file: ${error}`); + message.reply('Failed to download audio file.'); + return; + } - addToQueue(message.guild.id, tempFilePath, title); - playNextInQueue(message.guild.id, voiceChannel); + 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); + }); }); - }); + } } else { exec(`yt-dlp --dump-single-json "ytsearch:${searchQuery}"`, (error, stdout, stderr) => { if (error) { @@ -95,7 +153,7 @@ module.exports = { } const info = JSON.parse(stdout); - url = info.entries[0].webpage_url; + const url = info.entries[0].webpage_url; title = info.entries[0].title; tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); @@ -108,6 +166,8 @@ module.exports = { return; } + console.log(`Downloaded and saved: ${tempFilePath}`); + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Now Playing')