From 2660275306de0b007ac15f6fa3d4afef63bf7260 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sat, 17 Aug 2024 18:43:28 -0400 Subject: [PATCH] Try to fix incorecct time remaining --- commands/np.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/commands/np.js b/commands/np.js index 992d8d6..dc59f1f 100644 --- a/commands/np.js +++ b/commands/np.js @@ -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; + } } \ No newline at end of file