Thumbnail support

This commit is contained in:
Wizzard 2024-08-17 19:37:33 -04:00
parent 39d1d71f44
commit e9b144720d
3 changed files with 27 additions and 26 deletions

View File

@ -18,6 +18,11 @@ module.exports = {
return message.reply('There is no track currently playing.'); 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 currentTime = player.state?.resource?.playbackDuration || 0;
const totalDuration = await getTrackDuration(currentTrack.filePath); const totalDuration = await getTrackDuration(currentTrack.filePath);
const timeLeft = msToTime(totalDuration - currentTime); const timeLeft = msToTime(totalDuration - currentTime);
@ -26,6 +31,7 @@ module.exports = {
.setColor('#0099ff') .setColor('#0099ff')
.setTitle('Now Playing') .setTitle('Now Playing')
.setDescription(`**${currentTrack.title}**`) .setDescription(`**${currentTrack.title}**`)
.setThumbnail(currentTrack.thumbnailUrl || message.guild.iconURL())
.addFields( .addFields(
{ name: 'Time Left', value: timeLeft }, { name: 'Time Left', value: timeLeft },
) )

View File

@ -23,6 +23,7 @@ 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) {
@ -51,7 +52,7 @@ 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()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
return; return;
} else { } else {
@ -85,7 +86,7 @@ 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()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
return; return;
} else if (searchQuery.includes("cdn.discordapp.com")) { } else if (searchQuery.includes("cdn.discordapp.com")) {
@ -112,22 +113,26 @@ 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()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, null, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
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 ${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) { if (error) {
console.error(`Error getting title: ${error}`); console.error(`Error getting title or thumbnail: ${error}`);
message.reply('Failed to retrieve video title.'); message.reply('Failed to retrieve video title or thumbnail.');
return; 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 title: ${title}`);
console.log(`Retrieved thumbnail: ${thumbnailUrl}`);
loadingMessage = await message.channel.send(`**Loading...** ${title}`); loadingMessage = await message.channel.send(`**Loading...** ${title}`);
@ -150,13 +155,14 @@ 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()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, videoUrl, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
}); });
}); });
@ -181,6 +187,7 @@ module.exports = {
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}`);
@ -202,13 +209,14 @@ 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()); addToQueue(message.guild.id, tempFilePath, title, voiceChannel, url, message.author.username, message.author.displayAvatarURL(), thumbnailUrl);
playNextInQueue(message.guild.id); playNextInQueue(message.guild.id);
}); });
}); });

View File

@ -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 fs = require('fs');
const queueMap = new Map(); const queueMap = new Map();
@ -7,11 +7,11 @@ const currentTrackMap = new Map();
const repeatMap = new Map(); const repeatMap = new Map();
const voiceChannelMap = 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)) { if (!queueMap.has(guildId)) {
queueMap.set(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) { if (voiceChannel) {
voiceChannelMap.set(guildId, voiceChannel); voiceChannelMap.set(guildId, voiceChannel);
@ -88,25 +88,12 @@ function playNextInQueue(guildId) {
} }
function playTrack(guildId, voiceChannel, track) { function playTrack(guildId, voiceChannel, track) {
let connection = joinVoiceChannel({ const connection = joinVoiceChannel({
channelId: voiceChannel.id, channelId: voiceChannel.id,
guildId: guildId, guildId: guildId,
adapterCreator: voiceChannel.guild.voiceAdapterCreator, 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(); const audioPlayer = playerMap.get(guildId) || createAudioPlayer();
playerMap.set(guildId, audioPlayer); playerMap.set(guildId, audioPlayer);