Add now playing command

This commit is contained in:
Wizzard 2024-08-17 18:08:42 -04:00
parent 1af2e87809
commit 7a182a39dd
2 changed files with 59 additions and 1 deletions

54
commands/np.js Normal file
View File

@ -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;
}

View File

@ -38,6 +38,10 @@ function getQueue(guildId) {
return queueMap.get(guildId) || []; return queueMap.get(guildId) || [];
} }
function getPlayer(guildId) {
return playerMap.get(guildId);
}
function getCurrentTrack(guildId) { function getCurrentTrack(guildId) {
return currentTrackMap.get(guildId) || null; return currentTrackMap.get(guildId) || null;
} }
@ -155,4 +159,4 @@ function clearQueue(guildId) {
queueMap.set(guildId, []); queueMap.set(guildId, []);
} }
module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat, clearQueue, removeFromQueue }; module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat, clearQueue, removeFromQueue, getPlayer };