From 7a182a39dd9745033e81bb6a48338ed49d0511a8 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sat, 17 Aug 2024 18:08:42 -0400 Subject: [PATCH] Add now playing command --- commands/np.js | 54 +++++++++++++++++++++++++++++++++++++++++++ utils/queueManager.js | 6 ++++- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 commands/np.js diff --git a/commands/np.js b/commands/np.js new file mode 100644 index 0000000..118ddb7 --- /dev/null +++ b/commands/np.js @@ -0,0 +1,54 @@ +const { getCurrentTrack, getPlayer } = require('../utils/queueManager'); +const { EmbedBuilder } = require('discord.js'); +const fs = require('fs'); + +module.exports = { + name: 'np', + description: 'Show the currently playing track and time left', + execute(message) { + const currentTrack = getCurrentTrack(message.guild.id); + + if (!currentTrack) { + return message.reply('There is no track currently playing.'); + } + + const player = getPlayer(message.guild.id); + if (!player) { + return message.reply('There is no track currently playing.'); + } + + const currentTime = player.state?.resource?.playbackDuration || 0; + const totalDuration = getTrackDuration(currentTrack.filePath); + + const timeLeft = msToTime(totalDuration - currentTime); + + const embed = new EmbedBuilder() + .setColor('#0099ff') + .setTitle('Now Playing') + .setDescription(`**${currentTrack.title}**`) + .addFields( + { name: 'Time Left', value: timeLeft }, + ) + .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) + .setTimestamp(); + + message.channel.send({ embeds: [embed] }); + }, +}; + +function msToTime(duration) { + let seconds = Math.floor((duration / 1000) % 60), + minutes = Math.floor((duration / (1000 * 60)) % 60), + hours = Math.floor((duration / (1000 * 60 * 60)) % 24); + + hours = (hours < 10) ? "0" + hours : hours; + minutes = (minutes < 10) ? "0" + minutes : minutes; + seconds = (seconds < 10) ? "0" + seconds : seconds; + + return hours + ":" + minutes + ":" + seconds; +} + +function getTrackDuration(filePath) { + const stats = fs.statSync(filePath); + return stats.size / 32000 * 1000; +} \ No newline at end of file diff --git a/utils/queueManager.js b/utils/queueManager.js index 5d69cfd..d007ca5 100644 --- a/utils/queueManager.js +++ b/utils/queueManager.js @@ -38,6 +38,10 @@ function getQueue(guildId) { return queueMap.get(guildId) || []; } +function getPlayer(guildId) { + return playerMap.get(guildId); +} + function getCurrentTrack(guildId) { return currentTrackMap.get(guildId) || null; } @@ -155,4 +159,4 @@ function clearQueue(guildId) { queueMap.set(guildId, []); } -module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat, clearQueue, removeFromQueue }; \ No newline at end of file +module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat, clearQueue, removeFromQueue, getPlayer }; \ No newline at end of file