DZ-Musicbot/commands/play.js

72 lines
2.4 KiB
JavaScript
Raw Normal View History

2024-08-17 11:11:10 -04:00
const { addToQueue, playNextInQueue } = require('../utils/queueManager');
const ytDlpExec = require('yt-dlp-exec');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
2024-08-17 11:57:00 -04:00
const { EmbedBuilder } = require('discord.js');
2024-08-17 11:11:10 -04:00
module.exports = {
name: 'play',
description: 'Play a song from YouTube',
async execute(message, args) {
const searchQuery = args.join(' ');
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) {
return message.reply('You need to be in a voice channel to play music!');
}
if (!searchQuery) {
return message.reply('Please provide a YouTube link or a song name.');
}
2024-08-17 11:57:00 -04:00
let url, title;
try {
if (isValidURL(searchQuery)) {
url = searchQuery;
const info = await ytDlpExec(url, { dumpSingleJson: true });
title = info.title;
} else {
2024-08-17 11:11:10 -04:00
const searchResult = await ytDlpExec(`ytsearch:${searchQuery}`, {
dumpSingleJson: true,
noPlaylist: true,
format: 'bestaudio/best',
quiet: true,
});
2024-08-17 11:57:00 -04:00
url = searchResult.entries[0].webpage_url;
title = searchResult.entries[0].title;
2024-08-17 11:11:10 -04:00
}
2024-08-17 11:57:00 -04:00
const embed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle('Now Playing')
.setDescription(`**${title}**`)
.setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() });
message.channel.send({ embeds: [embed] });
const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
2024-08-17 11:11:10 -04:00
await ytDlpExec(url, {
cookies: path.join(__dirname, '../cookies.txt'),
format: 'bestaudio',
output: tempFilePath,
quiet: true,
});
2024-08-17 11:57:00 -04:00
2024-08-17 11:11:10 -04:00
addToQueue(message.guild.id, tempFilePath);
playNextInQueue(message.guild.id, voiceChannel);
2024-08-17 11:57:00 -04:00
2024-08-17 11:11:10 -04:00
} catch (error) {
console.error('yt-dlp error:', error);
2024-08-17 11:57:00 -04:00
message.reply('Failed to retrieve or download video. Please try again.');
2024-08-17 11:11:10 -04:00
}
},
};
function isValidURL(string) {
try {
new URL(string);
return true;
} catch (_) {
return false;
}
}