async function displayLyrics(message) {
const player = manager.players.get(message.guild.id);
if (!player?.playing) {
return message.channel.send('⚠️ No track is currently playing!');
}
const loadingMsg = await message.channel.send('🔍 Fetching lyrics...');
try {
const lyrics = await player.lyrics.getLyrics();
if (!lyrics || lyrics.loadType !== 'track') {
return loadingMsg.edit('❌ No lyrics found for this track.');
}
const { name, synced, data } = lyrics.data;
const currentTrack = player.current;
// Create embeds for paginated lyrics display
const embeds = [];
let currentPage = '';
let lineCount = 0;
for (const line of data) {
const timestamp = synced ? `\[${formatTimestamp(line.startTime)}\]` : '';
const lineText = `${timestamp}${line.text}\n`;
if ((currentPage + lineText).length > 4000 || lineCount >= 15) {
embeds.push({
title: `📝 Lyrics for ${currentTrack.title}`,
description: currentPage,
color: 0x3498db,
footer: {
text: `${synced ? 'Synchronized' : 'Unsynchronized'} lyrics | Page ${embeds.length + 1}`
}
});
currentPage = lineText;
lineCount = 1;
} else {
currentPage += lineText;
lineCount++;
}
}
// Add the last page if there's remaining content
if (currentPage.length > 0) {
embeds.push({
title: `📝 Lyrics for ${currentTrack.title}`,
description: currentPage,
color: 0x3498db,
footer: {
text: `${synced ? 'Synchronized' : 'Unsynchronized'} lyrics | Page ${embeds.length + 1}`
}
});
}
// Setup pagination buttons
const row = new Discord.ActionRowBuilder()
.addComponents(
new Discord.ButtonBuilder()
.setCustomId('prev')
.setLabel('Previous')
.setStyle('Primary')
.setEmoji('⬅️'),
new Discord.ButtonBuilder()
.setCustomId('next')
.setLabel('Next')
.setStyle('Primary')
.setEmoji('➡️')
);
let currentIndex = 0;
const initialMessage = await loadingMsg.edit({
content: null,
embeds: [embeds[0]],
components: [row]
});
// Handle pagination
const collector = initialMessage.createMessageComponentCollector({
time: 300000 // 5 minutes
});
collector.on('collect', async (interaction) => {
if (interaction.customId === 'prev') {
currentIndex = Math.max(0, currentIndex - 1);
} else if (interaction.customId === 'next') {
currentIndex = Math.min(embeds.length - 1, currentIndex + 1);
}
await interaction.update({
embeds: [embeds[currentIndex]],
components: [row]
});
});
collector.on('end', () => {
initialMessage.edit({ components: [] });
});
} catch (error) {
console.error('Error fetching lyrics:', error);
loadingMsg.edit('❌ An error occurred while fetching lyrics.');
}
}
// Helper function to format timestamps
function formatTimestamp(ms) {
const minutes = Math.floor(ms / 60000);
const seconds = ((ms % 60000) / 1000).toFixed(0);
return `${minutes}:${seconds.padStart(2, '0')}`;
}
// Command handler
client.on('messageCreate', async (message) => {
if (message.content === '!lyrics') {
await displayLyrics(message);
}
});