DZ-Musicbot/commands/play.js

227 lines
11 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-08-17 16:13:24 -04:00
const { exec } = require('child_process');
2024-08-17 11:11:10 -04:00
module.exports = {
name: 'play',
2024-08-17 12:26:42 -04:00
description: 'Play a song from YouTube, a URL, or an uploaded MP3 file',
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-08-17 11:11:10 -04:00
const searchQuery = args.join(' ');
const voiceChannel = message.member.voice.channel;
2024-08-17 17:05:26 -04:00
console.log(`Received command: play ${searchQuery}`);
console.log(`Voice channel: ${voiceChannel ? voiceChannel.name : 'None'}`);
2024-08-17 11:11:10 -04:00
if (!voiceChannel) {
2024-08-17 17:05:26 -04:00
console.error('User is not in a voice channel.');
2024-08-17 11:11:10 -04:00
return message.reply('You need to be in a voice channel to play music!');
}
2024-08-17 18:34:28 -04:00
let title, tempFilePath, videoUrl = null;
let loadingMessage;
2024-08-17 11:11:10 -04:00
2024-08-17 11:57:00 -04:00
try {
2024-08-17 12:26:42 -04:00
if (message.attachments.size > 0) {
const attachment = message.attachments.first();
if (attachment.name.endsWith('.mp3')) {
title = attachment.name;
2024-08-17 16:37:26 -04:00
tempFilePath = path.join(__dirname, '../utils/tmp', attachment.name);
console.log(`Attachment received: ${title}`);
console.log(`Downloading attachment to: ${tempFilePath}`);
2024-08-17 12:26:42 -04:00
const response = await fetch(attachment.url);
const buffer = await response.buffer();
fs.writeFileSync(tempFilePath, buffer);
2024-08-17 16:37:26 -04:00
console.log(`Downloaded and saved: ${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 12:26:42 -04:00
.setDescription(`**${title}**`)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
2024-08-17 17:05:26 -04:00
console.log('Adding to queue and attempting to play.');
2024-08-17 18:34:28 -04:00
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL());
2024-08-17 16:52:44 -04:00
playNextInQueue(message.guild.id);
2024-08-17 12:26:42 -04:00
return;
} else {
2024-08-17 17:05:26 -04:00
console.error('Attachment is not an MP3 file.');
2024-08-17 12:26:42 -04:00
return message.reply('Only MP3 files are supported for uploads.');
}
}
2024-08-17 11:57:00 -04:00
if (isValidURL(searchQuery)) {
2024-08-17 16:52:44 -04:00
if (searchQuery.endsWith('.mp3')) {
2024-08-17 16:37:26 -04:00
title = path.basename(searchQuery.split('?')[0]);
2024-08-17 16:52:44 -04:00
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
2024-08-17 12:26:42 -04:00
2024-08-17 16:52:44 -04:00
console.log(`MP3 link received: ${searchQuery}`);
console.log(`Downloading MP3 to: ${tempFilePath}`);
2024-08-17 16:13:24 -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 16:37:26 -04:00
console.log(`Downloaded and saved: ${tempFilePath}`);
const embed = new EmbedBuilder()
.setColor('#0099ff')
2024-08-17 19:15:09 -04:00
.setTitle('Added To Queue')
2024-08-17 16:37:26 -04:00
.setDescription(`**${title}**`)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
2024-08-17 17:05:26 -04:00
console.log('Adding to queue and attempting to play.');
2024-08-17 18:34:28 -04:00
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL());
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-08-17 16:37:26 -04:00
title = path.basename(searchQuery.split('?')[0]);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
2024-08-17 16:52:44 -04:00
console.log(`Discord MP3 link received: ${searchQuery}`);
console.log(`Downloading MP3 from Discord to: ${tempFilePath}`);
2024-08-17 16:37:26 -04:00
const response = await fetch(searchQuery);
2024-08-17 16:52:44 -04:00
if (!response.ok) throw new Error('Failed to download MP3 file from Discord.');
2024-08-17 16:37:26 -04:00
const buffer = await response.buffer();
fs.writeFileSync(tempFilePath, buffer);
console.log(`Downloaded and saved: ${tempFilePath}`);
const embed = new EmbedBuilder()
.setColor('#0099ff')
2024-08-17 19:15:09 -04:00
.setTitle('Added To Queue')
2024-08-17 16:37:26 -04:00
.setDescription(`**${title}**`)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
2024-08-17 17:05:26 -04:00
console.log('Adding to queue and attempting to play.');
2024-08-17 18:34:28 -04:00
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL());
2024-08-17 16:52:44 -04:00
playNextInQueue(message.guild.id);
2024-08-17 16:37:26 -04:00
return;
} else {
2024-08-17 17:05:26 -04:00
console.log(`YouTube link received: ${searchQuery}`);
2024-08-17 18:34:28 -04:00
videoUrl = searchQuery;
2024-08-17 18:36:59 -04:00
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title ${searchQuery}`, async (error, stdout, stderr) => {
2024-08-17 16:13:24 -04:00
if (error) {
2024-08-17 16:37:26 -04:00
console.error(`Error getting title: ${error}`);
message.reply('Failed to retrieve video title.');
2024-08-17 16:13:24 -04:00
return;
}
2024-08-17 16:37:26 -04:00
title = stdout.trim() || "Unknown Title";
console.log(`Retrieved title: ${title}`);
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
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
2024-08-17 18:34:28 -04:00
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${searchQuery}`, async (error, stdout, stderr) => {
2024-08-17 16:37:26 -04:00
if (error) {
console.error(`Error downloading file: ${error}`);
message.reply('Failed to download audio file.');
return;
}
2024-08-17 16:13:24 -04:00
2024-08-17 17:05:26 -04:00
console.log(`Downloaded and saved: ${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 16:37:26 -04:00
.setDescription(`**${title}**`)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
2024-08-17 17:05:26 -04:00
console.log('Adding to queue and attempting to play.');
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL());
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-08-17 17:05:26 -04:00
console.log(`Performing YouTube search: ${searchQuery}`);
2024-08-17 16:55:41 -04:00
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --dump-single-json "ytsearch:${searchQuery}"`, (error, stdout, stderr) => {
2024-08-17 16:13:24 -04:00
if (error) {
console.error(`Error searching: ${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-08-17 16:37:26 -04:00
const url = info.entries[0].webpage_url;
2024-08-17 16:13:24 -04:00
title = info.entries[0].title;
2024-08-17 12:26:42 -04:00
2024-08-17 16:13:24 -04:00
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
console.log(`Downloading file to: ${tempFilePath}`);
2024-08-17 18:34:28 -04:00
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${url}`, async (error, stdout, stderr) => {
2024-08-17 16:13:24 -04:00
if (error) {
console.error(`Error downloading file: ${error}`);
message.reply('Failed to download audio file.');
return;
}
2024-08-17 17:05:26 -04:00
console.log(`Downloaded and saved: ${tempFilePath}`);
2024-08-17 18:34:28 -04:00
if (loadingMessage) {
await loadingMessage.delete();
}
2024-08-17 16:13:24 -04:00
const embed = new EmbedBuilder()
.setColor('#0099ff')
2024-08-17 19:15:09 -04:00
.setTitle('Added To Queue')
2024-08-17 16:13:24 -04:00
.setDescription(`**${title}**`)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
message.channel.send({ embeds: [embed] });
2024-08-17 17:05:26 -04:00
console.log('Adding to queue and attempting to play.');
2024-08-17 18:34:28 -04:00
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL());
2024-08-17 16:52:44 -04:00
playNextInQueue(message.guild.id);
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
}
},
};
function isValidURL(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
2024-08-17 18:36:59 -04:00
}