const { addToQueue, playNextInQueue } = require('../utils/queueManager'); const { v4: uuidv4 } = require('uuid'); const path = require('path'); const { EmbedBuilder } = require('discord.js'); const fs = require('fs'); const { exec } = require('child_process'); const { execSync } = require('child_process'); module.exports = { name: 'play', description: 'Play a song from YouTube, a URL, or an uploaded media file', async execute(message, args) { const fetch = await import('node-fetch').then(module => module.default); const searchQuery = args.join(' '); const voiceChannel = message.member.voice.channel; console.log(`Received command: play ${searchQuery}`); console.log(`Voice channel: ${voiceChannel ? voiceChannel.name : 'None'}`); if (!voiceChannel) { console.error('User is not in a voice channel.'); return message.reply('You need to be in a voice channel to play music!'); } let title, tempFilePath, videoUrl = null; let loadingMessage; try { if (message.attachments.size > 0) { const attachment = message.attachments.first(); const extension = path.extname(attachment.name).toLowerCase(); if (['.mp3', '.mp4', '.webm', '.mov'].includes(extension)) { title = attachment.name; const originalFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); console.log(`Attachment received: ${title}`); console.log(`Downloading attachment to: ${originalFilePath}`); const response = await fetch(attachment.url); const buffer = await response.buffer(); fs.writeFileSync(originalFilePath, buffer); console.log(`Downloaded and saved: ${originalFilePath}`); if (extension !== '.mp3') { console.log(`Converting ${extension} to MP3...`); execSync(`ffmpeg -i "${originalFilePath}" -q:a 0 -map a "${tempFilePath}"`); fs.unlinkSync(originalFilePath); } else { tempFilePath = originalFilePath; } const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') .setDescription(`**${title}**`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL()); playNextInQueue(message.guild.id); return; } else { console.error('Attachment is not a supported file type.'); return message.reply('Only MP3, MP4, and WEBM files are supported for uploads.'); } } if (isValidURL(searchQuery)) { if (searchQuery.endsWith('.mp3')) { title = path.basename(searchQuery.split('?')[0]); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`); 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.'); const buffer = await response.buffer(); fs.writeFileSync(tempFilePath, buffer); console.log(`Downloaded and saved: ${tempFilePath}`); const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') .setDescription(`**${title}**`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); 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")) { title = path.basename(searchQuery.split('?')[0]); const originalFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); console.log(`Discord CDN link received: ${searchQuery}`); console.log(`Downloading file from Discord to: ${originalFilePath}`); const response = await fetch(searchQuery); if (!response.ok) throw new Error('Failed to download file from Discord.'); const buffer = await response.buffer(); fs.writeFileSync(originalFilePath, buffer); console.log(`Downloaded and saved: ${originalFilePath}`); if (!originalFilePath.endsWith('.mp3')) { console.log(`Converting to MP3...`); execSync(`ffmpeg -i "${originalFilePath}" -q:a 0 -map a "${tempFilePath}"`); fs.unlinkSync(originalFilePath); } else { tempFilePath = originalFilePath; } const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') .setDescription(`**${title}**`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL()); playNextInQueue(message.guild.id); return; } else { console.log(`YouTube link received: ${searchQuery}`); videoUrl = searchQuery; exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title ${searchQuery}`, async (error, stdout, stderr) => { if (error) { console.error(`Error getting title: ${error}`); message.reply('Failed to retrieve video title.'); return; } title = stdout.trim() || "Unknown Title"; console.log(`Retrieved title: ${title}`); loadingMessage = await message.channel.send(`**Loading...** ${title}`); 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) => { if (error) { console.error(`Error downloading file: ${error}`); message.reply('Failed to download audio file.'); return; } console.log(`Downloaded and saved: ${tempFilePath}`); if (loadingMessage) { await loadingMessage.delete(); } const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') .setDescription(`**${title}**`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); 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()); playNextInQueue(message.guild.id); }); }); } } 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) { console.error(`Error searching: ${error}`); 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; 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) => { if (error) { console.error(`Error downloading file: ${error}`); message.reply('Failed to download audio file.'); return; } console.log(`Downloaded and saved: ${tempFilePath}`); if (loadingMessage) { await loadingMessage.delete(); } const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') .setDescription(`**${title}**`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL()); playNextInQueue(message.guild.id); }); }); } } catch (error) { console.error('Error:', error); if (loadingMessage) await loadingMessage.delete(); message.reply('An error occurred while trying to play the music.'); } }, }; function isValidURL(string) { try { new URL(string); return true; } catch (_) { return false; } }