Update play.js
This commit is contained in:
parent
1bca1a8ed0
commit
f478894002
122
commands/play.js
122
commands/play.js
|
@ -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,45 +53,97 @@ 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')) {
|
||||||
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
title = path.basename(searchQuery.split('?')[0]);
|
||||||
|
tempFilePath = path.join(__dirname, '../utils/tmp', title);
|
||||||
|
|
||||||
exec(`yt-dlp --print title ${url}`, (error, stdout, stderr) => {
|
console.log(`Discord CDN link received: ${searchQuery}`);
|
||||||
if (error) {
|
console.log(`Downloading to: ${tempFilePath}`);
|
||||||
console.error(`Error getting title: ${error}`);
|
|
||||||
message.reply('Failed to retrieve video title.');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
title = stdout.trim();
|
const response = await fetch(searchQuery);
|
||||||
console.log(`Retrieved title: ${title}`);
|
if (!response.ok) throw new Error('Failed to download MP3 file.');
|
||||||
|
const buffer = await response.buffer();
|
||||||
|
fs.writeFileSync(tempFilePath, buffer);
|
||||||
|
|
||||||
// Now download the file
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --format bestaudio --output "${tempFilePath}" ${url}`, (error, stdout, stderr) => {
|
|
||||||
|
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`);
|
||||||
|
|
||||||
|
console.log(`YouTube link received: ${searchQuery}`);
|
||||||
|
|
||||||
|
exec(`yt-dlp --print title ${searchQuery}`, (error, stdout, stderr) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
console.error(`Error downloading file: ${error}`);
|
console.error(`Error getting title: ${error}`);
|
||||||
message.reply('Failed to download audio file.');
|
message.reply('Failed to retrieve video title.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
title = stdout.trim() || "Unknown Title";
|
||||||
.setColor('#0099ff')
|
console.log(`Retrieved title: ${title}`);
|
||||||
.setTitle('Now Playing')
|
|
||||||
.setDescription(`**${title}**`)
|
|
||||||
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
|
|
||||||
.setTimestamp();
|
|
||||||
|
|
||||||
message.channel.send({ embeds: [embed] });
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
addToQueue(message.guild.id, tempFilePath, title);
|
console.log(`Downloaded and saved: ${tempFilePath}`);
|
||||||
playNextInQueue(message.guild.id, voiceChannel);
|
|
||||||
|
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 {
|
} 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