DZ-Musicbot/commands/play.js

349 lines
16 KiB
JavaScript
Raw Normal View History

2024-08-17 11:11:10 -04:00
const { addToQueue, playNextInQueue } = require('../utils/queueManager');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
2024-08-17 11:57:00 -04:00
const { EmbedBuilder } = require('discord.js');
2024-08-17 12:26:42 -04:00
const fs = require('fs');
2024-09-08 12:35:31 -04:00
const { exec, execSync } = require('child_process');
2024-08-17 11:11:10 -04:00
2024-08-18 04:49:20 -04:00
const MAX_RETRIES = 3;
const RETRY_DELAY = 1000;
2024-09-08 17:00:25 -04:00
async function spawnFFmpegProcess(args, callback, retries = 0) {
2024-08-18 04:49:20 -04:00
const ffmpegProcess = spawn('ffmpeg', args);
ffmpegProcess.on('close', (code) => {
if (code === 0) {
callback(null);
} else if (retries < MAX_RETRIES) {
console.warn(`FFmpeg process failed, retrying (${retries + 1}/${MAX_RETRIES})...`);
setTimeout(() => {
spawnFFmpegProcess(args, callback, retries + 1);
}, RETRY_DELAY);
} else {
callback(new Error(`FFmpeg process failed after ${MAX_RETRIES} retries.`));
}
});
ffmpegProcess.on('error', (err) => {
if (retries < MAX_RETRIES) {
console.warn(`FFmpeg process error, retrying (${retries + 1}/${MAX_RETRIES})...`, err);
setTimeout(() => {
spawnFFmpegProcess(args, callback, retries + 1);
}, RETRY_DELAY);
} else {
callback(err);
}
});
}
2024-08-17 11:11:10 -04:00
module.exports = {
name: 'play',
2024-08-17 19:48:51 -04:00
description: 'Play a song from YouTube, a URL, or an uploaded media file',
2024-08-17 20:13:09 -04:00
aliases: ['p'],
2024-08-17 11:11:10 -04:00
async execute(message, args) {
2024-08-17 17:03:28 -04:00
const fetch = await import('node-fetch').then(module => module.default);
2024-09-08 17:00:25 -04:00
const limit = (await import('p-limit')).default(3);
2024-08-17 11:11:10 -04:00
const searchQuery = args.join(' ');
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.reply('You need to be in a voice channel to play music!');
}
2024-09-08 12:35:31 -04:00
const isPlaylist = searchQuery.includes("list=");
2024-08-17 18:34:28 -04:00
let loadingMessage;
2024-08-17 11:11:10 -04:00
2024-09-08 12:35:31 -04:00
const getDuration = (filePath) => {
try {
const output = execSync(`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${filePath}"`).toString().trim();
const durationSeconds = parseFloat(output);
const minutes = Math.floor(durationSeconds / 60);
const seconds = Math.floor(durationSeconds % 60);
return `${minutes}:${seconds.toString().padStart(2, '0')}`;
} catch (error) {
console.error('Error getting track duration:', error);
return 'Unknown';
}
};
2024-08-17 20:55:49 -04:00
2024-09-08 12:35:31 -04:00
try {
2024-08-17 12:26:42 -04:00
if (message.attachments.size > 0) {
const attachment = message.attachments.first();
2024-08-17 20:05:43 -04:00
const attachmentName = attachment.name.toLowerCase();
2024-08-17 12:26:42 -04:00
2024-08-17 20:05:43 -04:00
if (attachmentName.endsWith('.mp3')) {
2024-09-08 17:00:25 -04:00
const title = attachment.name;
const tempFilePath = path.join(__dirname, '../utils/tmp', attachment.name);
2024-08-17 16:37:26 -04:00
2024-08-17 12:26:42 -04:00
const response = await fetch(attachment.url);
const buffer = await response.buffer();
2024-08-17 20:05:43 -04:00
fs.writeFileSync(tempFilePath, buffer);
2024-08-17 19:48:51 -04:00
2024-08-17 20:55:49 -04:00
const duration = getDuration(tempFilePath);
2024-08-17 12:26:42 -04:00
const embed = new EmbedBuilder()
.setColor('#0099ff')
2024-08-17 19:15:09 -04:00
.setTitle('Added To Queue')
2024-08-17 20:55:49 -04:00
.setDescription(`**${title}** (${duration})`)
2024-08-17 12:26:42 -04:00
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
2024-08-17 20:05:43 -04:00
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
2024-08-17 16:52:44 -04:00
playNextInQueue(message.guild.id);
2024-08-17 20:05:43 -04:00
return;
} else if (attachmentName.endsWith('.mp4') || attachmentName.endsWith('.webm') || attachmentName.endsWith('.mov')) {
2024-09-08 17:00:25 -04:00
const title = attachment.name;
2024-08-17 20:05:43 -04:00
const convertedFileName = `${uuidv4()}.mp3`;
2024-09-08 17:00:25 -04:00
const tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
2024-08-17 20:05:43 -04:00
const response = await fetch(attachment.url);
const buffer = await response.buffer();
const tempVideoPath = path.join(__dirname, '../utils/tmp', attachment.name);
fs.writeFileSync(tempVideoPath, buffer);
2024-08-18 04:49:20 -04:00
spawnFFmpegProcess(['-i', tempVideoPath, '-f', 'mp3', '-ab', '192000', '-vn', tempFilePath], (err) => {
if (err) {
message.reply('Failed to convert the video file.');
return;
}
2024-08-17 20:05:43 -04:00
fs.unlinkSync(tempVideoPath);
2024-08-17 20:55:49 -04:00
const duration = getDuration(tempFilePath);
2024-08-17 20:05:43 -04:00
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Added To Queue')
2024-08-17 20:55:49 -04:00
.setDescription(`**${title}** (${duration})`)
2024-08-17 20:05:43 -04:00
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
playNextInQueue(message.guild.id);
});
2024-08-17 12:26:42 -04:00
return;
} else {
2024-08-17 20:05:43 -04:00
return message.reply('Only MP3, MP4, WEBM, and MOV files are supported for uploads.');
2024-08-17 12:26:42 -04:00
}
}
2024-08-17 11:57:00 -04:00
if (isValidURL(searchQuery)) {
2024-08-17 16:52:44 -04:00
if (searchQuery.endsWith('.mp3')) {
2024-09-08 17:00:25 -04:00
const title = path.basename(searchQuery.split('?')[0]);
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
2024-08-17 12:26:42 -04:00
2024-08-17 16:37:26 -04:00
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);
2024-08-17 16:13:24 -04:00
2024-08-17 20:55:49 -04:00
const duration = getDuration(tempFilePath);
2024-08-17 16:37:26 -04:00
const embed = new EmbedBuilder()
.setColor('#0099ff')
2024-08-17 19:15:09 -04:00
.setTitle('Added To Queue')
2024-08-17 20:55:49 -04:00
.setDescription(`**${title}** (${duration})`)
2024-08-17 16:37:26 -04:00
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
2024-08-17 20:05:43 -04:00
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
2024-08-17 16:52:44 -04:00
playNextInQueue(message.guild.id);
2024-08-17 16:37:26 -04:00
return;
2024-08-17 16:52:44 -04:00
} else if (searchQuery.includes("cdn.discordapp.com")) {
2024-09-08 17:00:25 -04:00
const title = path.basename(searchQuery.split('?')[0]);
2024-08-17 20:05:43 -04:00
const isVideo = searchQuery.endsWith('.mp4') || searchQuery.endsWith('.webm') || searchQuery.endsWith('.mov');
const fileExtension = isVideo ? 'mp3' : 'original';
const convertedFileName = `${uuidv4()}.${fileExtension}`;
2024-09-08 17:00:25 -04:00
const tempFilePath = path.join(__dirname, '../utils/tmp', convertedFileName);
2024-08-17 16:37:26 -04:00
const response = await fetch(searchQuery);
2024-08-17 20:05:43 -04:00
if (!response.ok) throw new Error('Failed to download media file from Discord.');
2024-08-17 16:37:26 -04:00
const buffer = await response.buffer();
2024-08-17 20:05:43 -04:00
if (isVideo) {
const tempVideoPath = path.join(__dirname, '../utils/tmp', path.basename(searchQuery.split('?')[0]));
fs.writeFileSync(tempVideoPath, buffer);
2024-08-18 04:49:20 -04:00
spawnFFmpegProcess(['-i', tempVideoPath, '-f', 'mp3', '-ab', '192000', '-vn', tempFilePath], (err) => {
if (err) {
message.reply('Failed to convert the video file.');
return;
}
2024-08-17 20:05:43 -04:00
fs.unlinkSync(tempVideoPath);
2024-08-17 20:55:49 -04:00
const duration = getDuration(tempFilePath);
2024-08-17 20:05:43 -04:00
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Added To Queue')
2024-08-17 20:55:49 -04:00
.setDescription(`**${title}** (${duration})`)
2024-08-17 20:05:43 -04:00
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
playNextInQueue(message.guild.id);
});
2024-08-17 19:48:51 -04:00
} else {
2024-08-17 20:05:43 -04:00
fs.writeFileSync(tempFilePath, buffer);
2024-08-17 16:37:26 -04:00
2024-08-17 20:55:49 -04:00
const duration = getDuration(tempFilePath);
2024-08-17 20:05:43 -04:00
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Added To Queue')
2024-08-17 20:55:49 -04:00
.setDescription(`**${title}** (${duration})`)
2024-08-17 20:05:43 -04:00
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
2024-08-17 16:37:26 -04:00
2024-08-17 20:05:43 -04:00
message.channel.send({ embeds: [embed] });
2024-08-17 16:37:26 -04:00
2024-08-17 20:05:43 -04:00
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), null);
playNextInQueue(message.guild.id);
}
2024-08-17 16:37:26 -04:00
return;
2024-09-08 12:35:31 -04:00
} else if (isPlaylist) {
loadingMessage = await message.channel.send(`Loading playlist...`);
2024-08-17 18:34:28 -04:00
2024-09-08 12:35:31 -04:00
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --flat-playlist --print-json "${searchQuery}"`, async (error, stdout) => {
if (error) {
message.reply('Failed to retrieve playlist info.');
return;
}
const playlist = stdout.split('\n').filter(Boolean).map(line => JSON.parse(line));
if (loadingMessage) await loadingMessage.delete();
message.channel.send(`Adding ${playlist.length} songs to the queue...`);
2024-09-08 17:00:25 -04:00
// Process the playlist with concurrency control using p-limit
const addToQueuePromises = playlist.map((video) => {
2024-09-08 12:35:31 -04:00
const videoUrl = `https://www.youtube.com/watch?v=${video.id}`;
2024-09-08 17:00:25 -04:00
return limit(() => addVideoToQueue(videoUrl, message, voiceChannel, false));
});
2024-09-08 12:35:31 -04:00
2024-09-08 17:00:25 -04:00
await Promise.all(addToQueuePromises); // Wait for all promises to complete with concurrency limit
2024-09-08 12:35:31 -04:00
playNextInQueue(message.guild.id);
});
} else {
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print-json --skip-download "${searchQuery}"`, async (error, stdout) => {
2024-08-17 16:13:24 -04:00
if (error) {
2024-08-17 20:05:43 -04:00
message.reply('Failed to retrieve video info.');
2024-08-17 16:13:24 -04:00
return;
}
2024-08-17 20:05:43 -04:00
const info = JSON.parse(stdout);
2024-09-08 17:00:25 -04:00
const title = info.title || "Unknown Title";
const thumbnailUrl = info.thumbnail || null;
2024-08-17 16:13:24 -04:00
2024-08-17 18:47:13 -04:00
loadingMessage = await message.channel.send(`**Loading...** ${title}`);
2024-08-17 18:36:59 -04:00
2024-09-08 17:00:25 -04:00
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
2024-08-17 18:36:59 -04:00
2024-09-08 17:00:25 -04:00
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" "${searchQuery}"`, async (error) => {
2024-08-17 16:37:26 -04:00
if (error) {
message.reply('Failed to download audio file.');
return;
}
2024-08-17 16:13:24 -04:00
2024-08-17 20:55:49 -04:00
const duration = getDuration(tempFilePath);
2024-08-17 18:34:28 -04:00
if (loadingMessage) {
await loadingMessage.delete();
}
2024-08-17 16:37:26 -04:00
const embed = new EmbedBuilder()
.setColor('#0099ff')
2024-08-17 19:15:09 -04:00
.setTitle('Added To Queue')
2024-08-17 20:55:49 -04:00
.setDescription(`**${title}** (${duration})`)
2024-08-17 20:05:43 -04:00
.setThumbnail(thumbnailUrl)
2024-08-17 16:37:26 -04:00
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
2024-09-08 12:35:31 -04:00
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, searchQuery, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
2024-08-17 16:52:44 -04:00
playNextInQueue(message.guild.id);
2024-08-17 16:37:26 -04:00
});
2024-08-17 16:13:24 -04:00
});
2024-08-17 16:37:26 -04:00
}
2024-08-17 11:57:00 -04:00
} else {
2024-08-17 18:34:28 -04:00
loadingMessage = await message.channel.send(`Searching for: **${searchQuery}**...`);
2024-09-08 12:35:31 -04:00
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --dump-single-json "ytsearch:${searchQuery}"`, async (error, stdout) => {
2024-08-17 16:13:24 -04:00
if (error) {
message.reply('Failed to search for video.');
return;
}
2024-08-17 15:58:45 -04:00
2024-08-17 16:13:24 -04:00
const info = JSON.parse(stdout);
2024-09-08 12:35:31 -04:00
const videoUrl = info.entries[0].webpage_url;
await addVideoToQueue(videoUrl, message, voiceChannel, true);
2024-08-17 16:13:24 -04:00
2024-09-08 12:35:31 -04:00
if (loadingMessage) await loadingMessage.delete();
2024-08-17 16:13:24 -04:00
});
2024-08-17 11:11:10 -04:00
}
} catch (error) {
2024-08-17 16:13:24 -04:00
console.error('Error:', error);
2024-08-17 18:34:28 -04:00
if (loadingMessage) await loadingMessage.delete();
2024-08-17 16:13:24 -04:00
message.reply('An error occurred while trying to play the music.');
2024-08-17 11:11:10 -04:00
}
},
};
2024-09-08 12:35:31 -04:00
async function addVideoToQueue(videoUrl, message, voiceChannel, sendEmbed = true) {
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print-json --skip-download "${videoUrl}"`, async (error, stdout) => {
if (error) {
message.reply('Failed to retrieve video info.');
return;
}
const info = JSON.parse(stdout);
const title = info.title;
const thumbnailUrl = info.thumbnail;
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" "${videoUrl}"`, async (error) => {
if (error) {
message.reply('Failed to download audio file.');
return;
}
if (sendEmbed) {
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Added To Queue')
.setDescription(`**${title}**`)
.setThumbnail(thumbnailUrl)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
}
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
playNextInQueue(message.guild.id);
});
});
}
2024-08-17 11:11:10 -04:00
function isValidURL(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
2024-09-08 17:00:25 -04:00
}