Allow most meida files

This commit is contained in:
Wizzard 2024-08-17 19:48:51 -04:00
parent e9b144720d
commit 496d6d5fe5
1 changed files with 43 additions and 37 deletions

View File

@ -4,10 +4,11 @@ const path = require('path');
const { EmbedBuilder } = require('discord.js'); const { EmbedBuilder } = require('discord.js');
const fs = require('fs'); const fs = require('fs');
const { exec } = require('child_process'); const { exec } = require('child_process');
const { execSync } = require('child_process');
module.exports = { module.exports = {
name: 'play', name: 'play',
description: 'Play a song from YouTube, a URL, or an uploaded MP3 file', description: 'Play a song from YouTube, a URL, or an uploaded media file',
async execute(message, args) { async execute(message, args) {
const fetch = await import('node-fetch').then(module => module.default); const fetch = await import('node-fetch').then(module => module.default);
const searchQuery = args.join(' '); const searchQuery = args.join(' ');
@ -23,24 +24,33 @@ module.exports = {
let title, tempFilePath, videoUrl = null; let title, tempFilePath, videoUrl = null;
let loadingMessage; let loadingMessage;
let thumbnailUrl = null;
try { try {
if (message.attachments.size > 0) { if (message.attachments.size > 0) {
const attachment = message.attachments.first(); const attachment = message.attachments.first();
const extension = path.extname(attachment.name).toLowerCase();
if (attachment.name.endsWith('.mp3')) { if (['.mp3', '.mp4', '.webm', '.mov'].includes(extension)) {
title = attachment.name; title = attachment.name;
tempFilePath = path.join(__dirname, '../utils/tmp', attachment.name); const originalFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
console.log(`Attachment received: ${title}`); console.log(`Attachment received: ${title}`);
console.log(`Downloading attachment to: ${tempFilePath}`); console.log(`Downloading attachment to: ${originalFilePath}`);
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(originalFilePath, buffer);
console.log(`Downloaded and saved: ${tempFilePath}`); console.log(`Downloaded and saved: ${originalFilePath}`);
if (extension !== '.mp3') {
console.log(`Converting ${extension} to MP3...`);
execSync(`ffmpeg -i "${originalFilePath}" -q:a 0 -map a "${tempFilePath}"`);
fs.unlinkSync(originalFilePath);
} else {
tempFilePath = originalFilePath;
}
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor('#0099ff') .setColor('#0099ff')
@ -52,12 +62,12 @@ 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, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); 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 {
console.error('Attachment is not an MP3 file.'); console.error('Attachment is not a supported file type.');
return message.reply('Only MP3 files are supported for uploads.'); return message.reply('Only MP3, MP4, and WEBM files are supported for uploads.');
} }
} }
@ -86,22 +96,31 @@ 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, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); 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")) {
title = path.basename(searchQuery.split('?')[0]); title = path.basename(searchQuery.split('?')[0]);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`); const originalFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}_${title}`);
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
console.log(`Discord MP3 link received: ${searchQuery}`); console.log(`Discord CDN link received: ${searchQuery}`);
console.log(`Downloading MP3 from Discord to: ${tempFilePath}`); console.log(`Downloading file from Discord to: ${originalFilePath}`);
const response = await fetch(searchQuery); const response = await fetch(searchQuery);
if (!response.ok) throw new Error('Failed to download MP3 file from Discord.'); if (!response.ok) throw new Error('Failed to download file from Discord.');
const buffer = await response.buffer(); const buffer = await response.buffer();
fs.writeFileSync(tempFilePath, buffer); fs.writeFileSync(originalFilePath, buffer);
console.log(`Downloaded and saved: ${tempFilePath}`); console.log(`Downloaded and saved: ${originalFilePath}`);
if (!originalFilePath.endsWith('.mp3')) {
console.log(`Converting to MP3...`);
execSync(`ffmpeg -i "${originalFilePath}" -q:a 0 -map a "${tempFilePath}"`);
fs.unlinkSync(originalFilePath);
} else {
tempFilePath = originalFilePath;
}
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor('#0099ff') .setColor('#0099ff')
@ -113,26 +132,22 @@ 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, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); 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 {
console.log(`YouTube link received: ${searchQuery}`); console.log(`YouTube link received: ${searchQuery}`);
videoUrl = searchQuery; videoUrl = searchQuery;
exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title --get-thumbnail ${searchQuery}`, async (error, stdout, stderr) => { exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title ${searchQuery}`, async (error, stdout, stderr) => {
if (error) { if (error) {
console.error(`Error getting title or thumbnail: ${error}`); console.error(`Error getting title: ${error}`);
message.reply('Failed to retrieve video title or thumbnail.'); message.reply('Failed to retrieve video title.');
return; return;
} }
const output = stdout.split('\n'); title = stdout.trim() || "Unknown Title";
title = output[0].trim() || "Unknown Title";
thumbnailUrl = output[1].trim();
console.log(`Retrieved title: ${title}`); console.log(`Retrieved title: ${title}`);
console.log(`Retrieved thumbnail: ${thumbnailUrl}`);
loadingMessage = await message.channel.send(`**Loading...** ${title}`); loadingMessage = await message.channel.send(`**Loading...** ${title}`);
@ -155,14 +170,13 @@ module.exports = {
.setColor('#0099ff') .setColor('#0099ff')
.setTitle('Added To Queue') .setTitle('Added To Queue')
.setDescription(`**${title}**`) .setDescription(`**${title}**`)
.setThumbnail(thumbnailUrl)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp(); .setTimestamp();
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(), thumbnailUrl); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL());
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
}); });
}); });
@ -179,15 +193,8 @@ module.exports = {
} }
const info = JSON.parse(stdout); const info = JSON.parse(stdout);
if (!info.entries || info.entries.length === 0) {
message.reply('No results found for your search.');
return;
}
const url = info.entries[0].webpage_url; const url = info.entries[0].webpage_url;
title = info.entries[0].title; title = info.entries[0].title;
thumbnailUrl = info.entries[0].thumbnail;
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}`);
@ -209,14 +216,13 @@ module.exports = {
.setColor('#0099ff') .setColor('#0099ff')
.setTitle('Added To Queue') .setTitle('Added To Queue')
.setDescription(`**${title}**`) .setDescription(`**${title}**`)
.setThumbnail(thumbnailUrl)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() })
.setTimestamp(); .setTimestamp();
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, url, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL());
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
}); });
}); });