Update play.js
This commit is contained in:
parent
1bca1a8ed0
commit
f478894002
|
@ -3,6 +3,7 @@ const { v4: uuidv4 } = require('uuid');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const { EmbedBuilder } = require('discord.js');
|
const { EmbedBuilder } = require('discord.js');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const fetch = require('node-fetch');
|
||||||
const { exec } = require('child_process');
|
const { exec } = require('child_process');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
@ -16,7 +17,7 @@ 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 url, title, tempFilePath;
|
let title, tempFilePath;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (message.attachments.size > 0) {
|
if (message.attachments.size > 0) {
|
||||||
|
@ -24,12 +25,17 @@ module.exports = {
|
||||||
|
|
||||||
if (attachment.name.endsWith('.mp3')) {
|
if (attachment.name.endsWith('.mp3')) {
|
||||||
title = attachment.name;
|
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 response = await fetch(attachment.url);
|
||||||
const buffer = await response.buffer();
|
const buffer = await response.buffer();
|
||||||
fs.writeFileSync(tempFilePath, buffer);
|
fs.writeFileSync(tempFilePath, buffer);
|
||||||
|
|
||||||
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setColor('#0099ff')
|
.setColor('#0099ff')
|
||||||
.setTitle('Now Playing')
|
.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)) {
|
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`);
|
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) {
|
if (error) {
|
||||||
console.error(`Error getting title: ${error}`);
|
console.error(`Error getting title: ${error}`);
|
||||||
message.reply('Failed to retrieve video title.');
|
message.reply('Failed to retrieve video title.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
title = stdout.trim();
|
title = stdout.trim() || "Unknown Title";
|
||||||
console.log(`Retrieved title: ${title}`);
|
console.log(`Retrieved title: ${title}`);
|
||||||
|
|
||||||
// Now download the file
|
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}" ${url}`, (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.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setColor('#0099ff')
|
.setColor('#0099ff')
|
||||||
.setTitle('Now Playing')
|
.setTitle('Now Playing')
|
||||||
|
@ -86,6 +143,7 @@ module.exports = {
|
||||||
playNextInQueue(message.guild.id, voiceChannel);
|
playNextInQueue(message.guild.id, voiceChannel);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
exec(`yt-dlp --dump-single-json "ytsearch:${searchQuery}"`, (error, stdout, stderr) => {
|
exec(`yt-dlp --dump-single-json "ytsearch:${searchQuery}"`, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
|
@ -95,7 +153,7 @@ module.exports = {
|
||||||
}
|
}
|
||||||
|
|
||||||
const info = JSON.parse(stdout);
|
const info = JSON.parse(stdout);
|
||||||
url = info.entries[0].webpage_url;
|
const url = info.entries[0].webpage_url;
|
||||||
title = info.entries[0].title;
|
title = info.entries[0].title;
|
||||||
|
|
||||||
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
||||||
|
@ -108,6 +166,8 @@ module.exports = {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
.setColor('#0099ff')
|
.setColor('#0099ff')
|
||||||
.setTitle('Now Playing')
|
.setTitle('Now Playing')
|
||||||
|
|
Loading…
Reference in New Issue