NATIvE NATIvE NATIvE NATIvE

This commit is contained in:
Wizzard 2024-08-17 16:13:24 -04:00
parent 4862446076
commit 1bca1a8ed0
1 changed files with 74 additions and 65 deletions

View File

@ -1,10 +1,9 @@
const { addToQueue, playNextInQueue } = require('../utils/queueManager');
const { exec } = require('child_process');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
const { EmbedBuilder } = require('discord.js');
const fetch = require('node-fetch');
const fs = require('fs');
const { exec } = require('child_process');
module.exports = {
name: 'play',
@ -48,49 +47,85 @@ module.exports = {
}
}
if (isValidURL(searchQuery)) {
url = searchQuery;
if (url.endsWith('.mp3')) {
title = path.basename(url);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
const response = await fetch(url);
const buffer = await response.buffer();
fs.writeFileSync(tempFilePath, buffer);
} else {
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
title = await downloadWithYtDlp(url, tempFilePath);
}
} else {
const searchUrl = `ytsearch:${searchQuery}`;
const searchResult = await getYtDlpInfo(searchUrl);
if (!searchResult || !searchResult.entries || searchResult.entries.length === 0) {
return message.reply('No results found for your search query.');
}
url = searchResult.entries[0].webpage_url;
title = searchResult.entries[0].title;
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
await downloadWithYtDlp(url, tempFilePath);
if (!searchQuery) {
return message.reply('Please provide a YouTube link, MP3 link, or a song name.');
}
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Now Playing')
.setDescription(`**${title}**`)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp();
if (isValidURL(searchQuery)) {
url = searchQuery;
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
message.channel.send({ embeds: [embed] });
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;
}
addToQueue(message.guild.id, tempFilePath, title);
playNextInQueue(message.guild.id, voiceChannel);
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);
});
});
} else {
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;
}
const info = JSON.parse(stdout);
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}`, (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);
});
});
}
} catch (error) {
console.error('yt-dlp error:', error);
message.reply('Failed to retrieve or download the video. Please try again.');
console.error('Error:', error);
message.reply('An error occurred while trying to play the music.');
}
},
};
@ -103,29 +138,3 @@ function isValidURL(string) {
return false;
}
}
function downloadWithYtDlp(url, output) {
return new Promise((resolve, reject) => {
const command = `yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output ${output} --quiet --verbose "${url}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
reject(`yt-dlp error: ${stderr}`);
} else {
resolve(output);
}
});
});
}
function getYtDlpInfo(url) {
return new Promise((resolve, reject) => {
const command = `yt-dlp --dump-single-json "${url}"`;
exec(command, (error, stdout, stderr) => {
if (error) {
reject(`yt-dlp info error: ${stderr}`);
} else {
resolve(JSON.parse(stdout));
}
});
});
}