NATIvE NATIvE NATIvE NATIvE
This commit is contained in:
parent
4862446076
commit
1bca1a8ed0
107
commands/play.js
107
commands/play.js
|
@ -1,10 +1,9 @@
|
||||||
const { addToQueue, playNextInQueue } = require('../utils/queueManager');
|
const { addToQueue, playNextInQueue } = require('../utils/queueManager');
|
||||||
const { exec } = require('child_process');
|
|
||||||
const { v4: uuidv4 } = require('uuid');
|
const { v4: uuidv4 } = require('uuid');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { EmbedBuilder } = require('discord.js');
|
const { EmbedBuilder } = require('discord.js');
|
||||||
const fetch = require('node-fetch');
|
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const { exec } = require('child_process');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'play',
|
name: 'play',
|
||||||
|
@ -48,32 +47,30 @@ module.exports = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!searchQuery) {
|
||||||
|
return message.reply('Please provide a YouTube link, MP3 link, or a song name.');
|
||||||
|
}
|
||||||
|
|
||||||
if (isValidURL(searchQuery)) {
|
if (isValidURL(searchQuery)) {
|
||||||
url = searchQuery;
|
url = searchQuery;
|
||||||
if (url.endsWith('.mp3')) {
|
|
||||||
title = path.basename(url);
|
|
||||||
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
||||||
|
|
||||||
const response = await fetch(url);
|
exec(`yt-dlp --print title ${url}`, (error, stdout, stderr) => {
|
||||||
const buffer = await response.buffer();
|
if (error) {
|
||||||
fs.writeFileSync(tempFilePath, buffer);
|
console.error(`Error getting title: ${error}`);
|
||||||
} else {
|
message.reply('Failed to retrieve video title.');
|
||||||
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
return;
|
||||||
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 = stdout.trim();
|
||||||
title = searchResult.entries[0].title;
|
console.log(`Retrieved title: ${title}`);
|
||||||
|
|
||||||
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
// Now download the file
|
||||||
await downloadWithYtDlp(url, 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()
|
const embed = new EmbedBuilder()
|
||||||
|
@ -87,10 +84,48 @@ module.exports = {
|
||||||
|
|
||||||
addToQueue(message.guild.id, tempFilePath, title);
|
addToQueue(message.guild.id, tempFilePath, title);
|
||||||
playNextInQueue(message.guild.id, voiceChannel);
|
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) {
|
} catch (error) {
|
||||||
console.error('yt-dlp error:', error);
|
console.error('Error:', error);
|
||||||
message.reply('Failed to retrieve or download the video. Please try again.');
|
message.reply('An error occurred while trying to play the music.');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -103,29 +138,3 @@ function isValidURL(string) {
|
||||||
return false;
|
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));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue