Embed title of video

This commit is contained in:
Wizzard 2024-08-17 11:57:00 -04:00
parent 46b937a107
commit e8486e9324
1 changed files with 22 additions and 13 deletions

View File

@ -2,6 +2,7 @@ const { addToQueue, playNextInQueue } = require('../utils/queueManager');
const ytDlpExec = require('yt-dlp-exec'); 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');
module.exports = { module.exports = {
name: 'play', name: 'play',
@ -18,37 +19,45 @@ module.exports = {
return message.reply('Please provide a YouTube link or a song name.'); return message.reply('Please provide a YouTube link or a song name.');
} }
let url; let url, title;
try {
if (isValidURL(searchQuery)) { if (isValidURL(searchQuery)) {
url = searchQuery; url = searchQuery;
const info = await ytDlpExec(url, { dumpSingleJson: true });
title = info.title;
} else { } else {
try {
const searchResult = await ytDlpExec(`ytsearch:${searchQuery}`, { const searchResult = await ytDlpExec(`ytsearch:${searchQuery}`, {
dumpSingleJson: true, dumpSingleJson: true,
noPlaylist: true, noPlaylist: true,
format: 'bestaudio/best', format: 'bestaudio/best',
quiet: true, quiet: true,
}); });
url = searchResult.webpage_url; url = searchResult.entries[0].webpage_url;
} catch (error) { title = searchResult.entries[0].title;
console.error('yt-dlp search error:', error);
return message.reply('Could not find the song. Please try again.');
}
} }
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`); const tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`);
try {
await ytDlpExec(url, { await ytDlpExec(url, {
cookies: path.join(__dirname, '../cookies.txt'), cookies: path.join(__dirname, '../cookies.txt'),
format: 'bestaudio', format: 'bestaudio',
output: tempFilePath, output: tempFilePath,
quiet: true, quiet: true,
}); });
addToQueue(message.guild.id, tempFilePath); addToQueue(message.guild.id, tempFilePath);
playNextInQueue(message.guild.id, voiceChannel); playNextInQueue(message.guild.id, voiceChannel);
} catch (error) { } catch (error) {
console.error('yt-dlp error:', error); console.error('yt-dlp error:', error);
message.reply('Failed to download video with yt-dlp.'); message.reply('Failed to retrieve or download video. Please try again.');
} }
}, },
}; };