Try to fix incorecct time remaining

This commit is contained in:
Wizzard 2024-08-17 18:43:28 -04:00
parent 0641ccfb0d
commit 2660275306
1 changed files with 11 additions and 5 deletions

View File

@ -1,11 +1,12 @@
const { getCurrentTrack, getPlayer } = require('../utils/queueManager');
const { EmbedBuilder } = require('discord.js');
const fs = require('fs');
const { execSync } = require('child_process');
module.exports = {
name: 'np',
description: 'Show the currently playing track and time left',
execute(message) {
async execute(message) {
const currentTrack = getCurrentTrack(message.guild.id);
if (!currentTrack) {
@ -18,7 +19,7 @@ module.exports = {
}
const currentTime = player.state?.resource?.playbackDuration || 0;
const totalDuration = getTrackDuration(currentTrack.filePath);
const totalDuration = await getTrackDuration(currentTrack.filePath);
const timeLeft = msToTime(totalDuration - currentTime);
const embed = new EmbedBuilder()
@ -47,7 +48,12 @@ function msToTime(duration) {
return hours + ":" + minutes + ":" + seconds;
}
function getTrackDuration(filePath) {
const stats = fs.statSync(filePath);
return stats.size / 32000 * 1000;
async function getTrackDuration(filePath) {
try {
const output = execSync(`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${filePath}"`).toString().trim();
return parseFloat(output) * 1000;
} catch (error) {
console.error('Error getting track duration:', error);
return 0;
}
}