DZ-Musicbot/commands/np.js

53 lines
1.8 KiB
JavaScript

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 ${currentTrack.requester}`, iconURL: currentTrack.avatarURL })
.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;
}