diff --git a/commands/np.js b/commands/np.js index dc59f1f..6f846b8 100644 --- a/commands/np.js +++ b/commands/np.js @@ -18,6 +18,11 @@ module.exports = { return message.reply('There is no track currently playing.'); } + if (!fs.existsSync(currentTrack.filePath)) { + console.error('Current track file not found:', currentTrack.filePath); + return message.reply('The current track file is missing.'); + } + const currentTime = player.state?.resource?.playbackDuration || 0; const totalDuration = await getTrackDuration(currentTrack.filePath); const timeLeft = msToTime(totalDuration - currentTime); @@ -26,6 +31,7 @@ module.exports = { .setColor('#0099ff') .setTitle('Now Playing') .setDescription(`**${currentTrack.title}**`) + .setThumbnail(currentTrack.thumbnailUrl || message.guild.iconURL()) .addFields( { name: 'Time Left', value: timeLeft }, ) diff --git a/commands/play.js b/commands/play.js index aba6c56..84a9d91 100644 --- a/commands/play.js +++ b/commands/play.js @@ -23,6 +23,7 @@ module.exports = { let title, tempFilePath, videoUrl = null; let loadingMessage; + let thumbnailUrl = null; try { if (message.attachments.size > 0) { @@ -51,7 +52,7 @@ module.exports = { message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); playNextInQueue(message.guild.id); return; } else { @@ -85,7 +86,7 @@ module.exports = { message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); playNextInQueue(message.guild.id); return; } else if (searchQuery.includes("cdn.discordapp.com")) { @@ -112,22 +113,26 @@ module.exports = { message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); playNextInQueue(message.guild.id); return; } else { console.log(`YouTube link received: ${searchQuery}`); videoUrl = searchQuery; - exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title ${searchQuery}`, async (error, stdout, stderr) => { + exec(`yt-dlp --cookies ${path.join(__dirname, '../cookies.txt')} --print title --get-thumbnail ${searchQuery}`, async (error, stdout, stderr) => { if (error) { - console.error(`Error getting title: ${error}`); - message.reply('Failed to retrieve video title.'); + console.error(`Error getting title or thumbnail: ${error}`); + message.reply('Failed to retrieve video title or thumbnail.'); return; } - title = stdout.trim() || "Unknown Title"; + const output = stdout.split('\n'); + title = output[0].trim() || "Unknown Title"; + thumbnailUrl = output[1].trim(); + console.log(`Retrieved title: ${title}`); + console.log(`Retrieved thumbnail: ${thumbnailUrl}`); loadingMessage = await message.channel.send(`**Loading...** ${title}`); @@ -150,13 +155,14 @@ module.exports = { .setColor('#0099ff') .setTitle('Added To Queue') .setDescription(`**${title}**`) + .setThumbnail(thumbnailUrl) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); playNextInQueue(message.guild.id); }); }); @@ -181,6 +187,7 @@ module.exports = { const url = info.entries[0].webpage_url; title = info.entries[0].title; + thumbnailUrl = info.entries[0].thumbnail; tempFilePath = path.join(__dirname, '../utils/tmp', `${uuidv4()}.mp3`); console.log(`Downloading file to: ${tempFilePath}`); @@ -202,13 +209,14 @@ module.exports = { .setColor('#0099ff') .setTitle('Added To Queue') .setDescription(`**${title}**`) + .setThumbnail(thumbnailUrl) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); message.channel.send({ embeds: [embed] }); console.log('Adding to queue and attempting to play.'); - addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL()); + addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL(), thumbnailUrl); playNextInQueue(message.guild.id); }); }); diff --git a/utils/queueManager.js b/utils/queueManager.js index ba5d27e..e03cd79 100644 --- a/utils/queueManager.js +++ b/utils/queueManager.js @@ -1,4 +1,4 @@ -const { createAudioPlayer, createAudioResource, AudioPlayerStatus, joinVoiceChannel, VoiceConnectionStatus } = require('@discordjs/voice'); +const { createAudioPlayer, createAudioResource, AudioPlayerStatus, joinVoiceChannel } = require('@discordjs/voice'); const fs = require('fs'); const queueMap = new Map(); @@ -7,11 +7,11 @@ const currentTrackMap = new Map(); const repeatMap = new Map(); const voiceChannelMap = new Map(); -function addToQueue(guildId, filePath, title, voiceChannel, url, requester, avatarURL) { +function addToQueue(guildId, filePath, title, voiceChannel, url, requester, avatarURL, thumbnailUrl) { if (!queueMap.has(guildId)) { queueMap.set(guildId, []); } - queueMap.get(guildId).push({ filePath, title, url, requester, avatarURL }); + queueMap.get(guildId).push({ filePath, title, url, requester, avatarURL, thumbnailUrl }); if (voiceChannel) { voiceChannelMap.set(guildId, voiceChannel); @@ -88,25 +88,12 @@ function playNextInQueue(guildId) { } function playTrack(guildId, voiceChannel, track) { - let connection = joinVoiceChannel({ + const connection = joinVoiceChannel({ channelId: voiceChannel.id, guildId: guildId, adapterCreator: voiceChannel.guild.voiceAdapterCreator, }); - connection.on(VoiceConnectionStatus.Disconnected, async (oldState, newState) => { - try { - await Promise.race([ - entersState(connection, VoiceConnectionStatus.Signalling, 5_000), - entersState(connection, VoiceConnectionStatus.Connecting, 5_000), - ]); - } catch (error) { - connection.destroy(); - currentTrackMap.delete(guildId); - queueMap.delete(guildId); - } - }); - const audioPlayer = playerMap.get(guildId) || createAudioPlayer(); playerMap.set(guildId, audioPlayer);