Allow playback of local files
This commit is contained in:
parent
dc5d2fd19b
commit
d7bdc851fe
|
@ -3,10 +3,12 @@ const ytDlpExec = require('yt-dlp-exec');
|
||||||
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');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'play',
|
name: 'play',
|
||||||
description: 'Play a song from YouTube',
|
description: 'Play a song from YouTube, a URL, or an uploaded MP3 file',
|
||||||
async execute(message, args) {
|
async execute(message, args) {
|
||||||
const searchQuery = args.join(' ');
|
const searchQuery = args.join(' ');
|
||||||
const voiceChannel = message.member.voice.channel;
|
const voiceChannel = message.member.voice.channel;
|
||||||
|
@ -15,16 +17,63 @@ 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!');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!searchQuery) {
|
let url, title, tempFilePath;
|
||||||
return message.reply('Please provide a YouTube link or a song name.');
|
|
||||||
}
|
|
||||||
|
|
||||||
let url, title;
|
|
||||||
try {
|
try {
|
||||||
|
if (message.attachments.size > 0) {
|
||||||
|
const attachment = message.attachments.first();
|
||||||
|
|
||||||
|
if (attachment.name.endsWith('.mp3')) {
|
||||||
|
title = attachment.name;
|
||||||
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
||||||
|
|
||||||
|
const response = await fetch(attachment.url);
|
||||||
|
const buffer = await response.buffer();
|
||||||
|
fs.writeFileSync(tempFilePath, buffer);
|
||||||
|
|
||||||
|
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 {
|
||||||
|
return message.reply('Only MP3 files are supported for uploads.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
const info = await ytDlpExec(url, { dumpSingleJson: true });
|
|
||||||
title = info.title;
|
if (url.endsWith('.mp3')) {
|
||||||
|
title = path.basename(url);
|
||||||
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
||||||
|
|
||||||
|
const response = await fetch(url);
|
||||||
|
const buffer = await response.buffer();
|
||||||
|
fs.writeFileSync(tempFilePath, buffer);
|
||||||
|
} else {
|
||||||
|
const info = await ytDlpExec(url, { dumpSingleJson: true });
|
||||||
|
title = info.title;
|
||||||
|
|
||||||
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
||||||
|
await ytDlpExec(url, {
|
||||||
|
cookies: path.join(__dirname, '../cookies.txt'),
|
||||||
|
format: 'bestaudio',
|
||||||
|
output: tempFilePath,
|
||||||
|
quiet: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const searchResult = await ytDlpExec(`ytsearch:${searchQuery}`, {
|
const searchResult = await ytDlpExec(`ytsearch:${searchQuery}`, {
|
||||||
dumpSingleJson: true,
|
dumpSingleJson: true,
|
||||||
|
@ -34,6 +83,14 @@ module.exports = {
|
||||||
});
|
});
|
||||||
url = searchResult.entries[0].webpage_url;
|
url = searchResult.entries[0].webpage_url;
|
||||||
title = searchResult.entries[0].title;
|
title = searchResult.entries[0].title;
|
||||||
|
|
||||||
|
tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
||||||
|
await ytDlpExec(url, {
|
||||||
|
cookies: path.join(__dirname, '../cookies.txt'),
|
||||||
|
format: 'bestaudio',
|
||||||
|
output: tempFilePath,
|
||||||
|
quiet: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const embed = new EmbedBuilder()
|
const embed = new EmbedBuilder()
|
||||||
|
@ -41,19 +98,11 @@ module.exports = {
|
||||||
.setTitle('Now Playing')
|
.setTitle('Now Playing')
|
||||||
.setDescription(`**${title}**`)
|
.setDescription(`**${title}**`)
|
||||||
.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] });
|
||||||
|
|
||||||
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
|
addToQueue(message.guild.id, tempFilePath, title);
|
||||||
await ytDlpExec(url, {
|
|
||||||
cookies: path.join(__dirname, '../cookies.txt'),
|
|
||||||
format: 'bestaudio',
|
|
||||||
output: tempFilePath,
|
|
||||||
quiet: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
addToQueue(message.guild.id, tempFilePath);
|
|
||||||
playNextInQueue(message.guild.id, voiceChannel);
|
playNextInQueue(message.guild.id, voiceChannel);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
Loading…
Reference in New Issue