DZ-Musicbot/commands/play.js

141 lines
5.8 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) {
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-08-17 12:26:42 -04:00
let url, title, tempFilePath;
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;
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
const response = await fetch(attachment.url);
const buffer = await response.buffer();
fs.writeFileSync(tempFilePath, buffer);
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 {
return message.reply('Only MP3 files are supported for uploads.');
}
}
2024-08-17 16:13:24 -04:00
if (!searchQuery) {
return message.reply('Please provide a YouTube link, MP3 link, or a song name.');
}
2024-08-17 11:57:00 -04:00
if (isValidURL(searchQuery)) {
url = searchQuery;
2024-08-17 16:13:24 -04:00
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
2024-08-17 12:26:42 -04:00
2024-08-17 16:13:24 -04:00
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;
}
title = stdout.trim();
console.log(`Retrieved title: ${title}`);
// Now download the file
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${url}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error downloading file: ${error}`);
message.reply('Failed to download audio file.');
return;
}
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);
});
});
2024-08-17 11:57:00 -04:00
} else {
2024-08-17 16:13:24 -04:00
exec(`yt-dlp --dump-single-json "ytsearch:${searchQuery}"`, (error, stdout, stderr) => {
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);
url = info.entries[0].webpage_url;
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}`);
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${url}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error downloading file: ${error}`);
message.reply('Failed to download audio file.');
return;
}
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);
});
});
2024-08-17 11:11:10 -04:00
}
} catch (error) {
2024-08-17 16:13:24 -04:00
console.error('Error:', error);
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 15:58:45 -04:00
}