Update play.js

This commit is contained in:
Wizzard 2024-08-17 16:37:26 -04:00
parent 1bca1a8ed0
commit f478894002
1 changed files with 91 additions and 31 deletions

View File

@ -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,32 +53,83 @@ module.exports = {
}
}
if (!searchQuery) {
return message.reply('Please provide a YouTube link, MP3 link, or a song name.');
}
if (isValidURL(searchQuery)) {
url = searchQuery;
if (searchQuery.includes('cdn.discordapp.com')) {
title = path.basename(searchQuery.split('?')[0]);
tempFilePath = path.join(__dirname, '../utils/tmp', title);
console.log(`Discord CDN 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 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`);
exec(`yt-dlp --print title ${url}`, (error, stdout, stderr) => {
console.log(`YouTube link received: ${searchQuery}`);
exec(`yt-dlp --print title ${searchQuery}`, (error, stdout, stderr) => {
if (error) {
console.error(`Error getting title: ${error}`);
message.reply('Failed to retrieve video title.');
return;
}
title = stdout.trim();
title = stdout.trim() || "Unknown Title";
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) => {
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;
}
console.log(`Downloaded and saved: ${tempFilePath}`);
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Now Playing')
@ -86,6 +143,7 @@ module.exports = {
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')