Update play.js and queue.js

This commit is contained in:
Wizzard 2024-08-17 18:34:28 -04:00
parent 0f4fdb8335
commit fc19af32a5
2 changed files with 24 additions and 10 deletions

View File

@ -21,7 +21,8 @@ module.exports = {
return message.reply('You need to be in a voice channel to play music!'); return message.reply('You need to be in a voice channel to play music!');
} }
let title, tempFilePath, videoUrl; let title, tempFilePath, videoUrl = null;
let loadingMessage;
try { try {
if (message.attachments.size > 0) { if (message.attachments.size > 0) {
@ -50,7 +51,7 @@ module.exports = {
message.channel.send({ embeds: [embed] }); message.channel.send({ embeds: [embed] });
console.log('Adding to queue and attempting to play.'); console.log('Adding to queue and attempting to play.');
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL());
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
return; return;
} else { } else {
@ -84,7 +85,7 @@ module.exports = {
message.channel.send({ embeds: [embed] }); message.channel.send({ embeds: [embed] });
console.log('Adding to queue and attempting to play.'); console.log('Adding to queue and attempting to play.');
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL());
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
return; return;
} else if (searchQuery.includes("cdn.discordapp.com")) { } else if (searchQuery.includes("cdn.discordapp.com")) {
@ -111,14 +112,17 @@ module.exports = {
message.channel.send({ embeds: [embed] }); message.channel.send({ embeds: [embed] });
console.log('Adding to queue and attempting to play.'); console.log('Adding to queue and attempting to play.');
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL());
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
return; return;
} else { } else {
loadingMessage = await message.channel.send(`**Loading**...`);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
videoUrl = searchQuery;
console.log(`YouTube link received: ${searchQuery}`); console.log(`YouTube link received: ${searchQuery}`);
videoUrl = searchQuery;
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title ${searchQuery}`, (error, stdout, stderr) => { exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title ${searchQuery}`, (error, stdout, stderr) => {
if (error) { if (error) {
console.error(`Error getting title: ${error}`); console.error(`Error getting title: ${error}`);
@ -129,7 +133,7 @@ module.exports = {
title = stdout.trim() || "Unknown Title"; title = stdout.trim() || "Unknown Title";
console.log(`Retrieved title: ${title}`); console.log(`Retrieved title: ${title}`);
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${searchQuery}`, (error, stdout, stderr) => { exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${searchQuery}`, async (error, stdout, stderr) => {
if (error) { if (error) {
console.error(`Error downloading file: ${error}`); console.error(`Error downloading file: ${error}`);
message.reply('Failed to download audio file.'); message.reply('Failed to download audio file.');
@ -138,6 +142,10 @@ module.exports = {
console.log(`Downloaded and saved: ${tempFilePath}`); console.log(`Downloaded and saved: ${tempFilePath}`);
if (loadingMessage) {
await loadingMessage.delete();
}
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor('#0099ff') .setColor('#0099ff')
.setTitle('Now Playing') .setTitle('Now Playing')
@ -154,6 +162,8 @@ module.exports = {
}); });
} }
} else { } else {
loadingMessage = await message.channel.send(`Searching for: **${searchQuery}**...`);
console.log(`Performing YouTube search: ${searchQuery}`); console.log(`Performing YouTube search: ${searchQuery}`);
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --dump-single-json "ytsearch:${searchQuery}"`, (error, stdout, stderr) => { exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --dump-single-json "ytsearch:${searchQuery}"`, (error, stdout, stderr) => {
if (error) { if (error) {
@ -165,12 +175,11 @@ module.exports = {
const info = JSON.parse(stdout); const info = JSON.parse(stdout);
const url = info.entries[0].webpage_url; const url = info.entries[0].webpage_url;
title = info.entries[0].title; title = info.entries[0].title;
videoUrl = url;
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
console.log(`Downloading file to: ${tempFilePath}`); console.log(`Downloading file to: ${tempFilePath}`);
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}" ${url}`, async (error, stdout, stderr) => {
if (error) { if (error) {
console.error(`Error downloading file: ${error}`); console.error(`Error downloading file: ${error}`);
message.reply('Failed to download audio file.'); message.reply('Failed to download audio file.');
@ -179,6 +188,10 @@ module.exports = {
console.log(`Downloaded and saved: ${tempFilePath}`); console.log(`Downloaded and saved: ${tempFilePath}`);
if (loadingMessage) {
await loadingMessage.delete();
}
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor('#0099ff') .setColor('#0099ff')
.setTitle('Now Playing') .setTitle('Now Playing')
@ -189,7 +202,7 @@ module.exports = {
message.channel.send({ embeds: [embed] }); message.channel.send({ embeds: [embed] });
console.log('Adding to queue and attempting to play.'); console.log('Adding to queue and attempting to play.');
addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL());
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
}); });
}); });
@ -197,6 +210,7 @@ module.exports = {
} catch (error) { } catch (error) {
console.error('Error:', error); console.error('Error:', error);
if (loadingMessage) await loadingMessage.delete();
message.reply('An error occurred while trying to play the music.'); message.reply('An error occurred while trying to play the music.');
} }
}, },