From b1bc4306ebf8a9705f90a910c8cdd7074af72e6f Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sat, 17 Aug 2024 20:55:49 -0400 Subject: [PATCH] Show duration --- commands/play.js | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/commands/play.js b/commands/play.js index 9ba568b..f987151 100644 --- a/commands/play.js +++ b/commands/play.js @@ -3,7 +3,7 @@ const { v4: uuidv4 } = require('uuid'); const path = require('path'); const { EmbedBuilder } = require('discord.js'); const fs = require('fs'); -const { exec, spawn } = require('child_process'); +const { exec, spawn, execSync } = require('child_process'); module.exports = { name: 'play', @@ -26,6 +26,19 @@ module.exports = { let loadingMessage; try { + const getDuration = (filePath) => { + try { + const output = execSync(`ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "${filePath}"`).toString().trim(); + const durationSeconds = parseFloat(output); + const minutes = Math.floor(durationSeconds / 60); + const seconds = Math.floor(durationSeconds % 60); + return `${minutes}:${seconds.toString().padStart(2, '0')}`; + } catch (error) { + console.error('Error getting track duration:', error); + return 'Unknown'; + } + }; + if (message.attachments.size > 0) { const attachment = message.attachments.first(); const attachmentName = attachment.name.toLowerCase(); @@ -43,10 +56,12 @@ module.exports = { console.log(`Downloaded and saved: ${tempFilePath}`); + const duration = getDuration(tempFilePath); + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') - .setDescription(`**${title}**`) + .setDescription(`**${title}** (${duration})`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); @@ -81,10 +96,12 @@ module.exports = { console.log(`Converted and saved: ${tempFilePath}`); fs.unlinkSync(tempVideoPath); + const duration = getDuration(tempFilePath); + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') - .setDescription(`**${title}**`) + .setDescription(`**${title}** (${duration})`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); @@ -122,10 +139,12 @@ module.exports = { console.log(`Downloaded and saved: ${tempFilePath}`); + const duration = getDuration(tempFilePath); + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') - .setDescription(`**${title}**`) + .setDescription(`**${title}** (${duration})`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); @@ -165,10 +184,12 @@ module.exports = { console.log(`Converted and saved: ${tempFilePath}`); fs.unlinkSync(tempVideoPath); + const duration = getDuration(tempFilePath); + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') - .setDescription(`**${title}**`) + .setDescription(`**${title}** (${duration})`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); @@ -188,10 +209,12 @@ module.exports = { fs.writeFileSync(tempFilePath, buffer); console.log(`Downloaded and saved: ${tempFilePath}`); + const duration = getDuration(tempFilePath); + const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') - .setDescription(`**${title}**`) + .setDescription(`**${title}** (${duration})`) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); @@ -233,6 +256,8 @@ module.exports = { console.log(`Downloaded and saved: ${tempFilePath}`); + const duration = getDuration(tempFilePath); + if (loadingMessage) { await loadingMessage.delete(); } @@ -240,7 +265,7 @@ module.exports = { const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') - .setDescription(`**${title}**`) + .setDescription(`**${title}** (${duration})`) .setThumbnail(thumbnailUrl) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp(); @@ -281,6 +306,8 @@ module.exports = { console.log(`Downloaded and saved: ${tempFilePath}`); + const duration = getDuration(tempFilePath); + if (loadingMessage) { await loadingMessage.delete(); } @@ -288,7 +315,7 @@ module.exports = { const embed = new EmbedBuilder() .setColor('#0099ff') .setTitle('Added To Queue') - .setDescription(`**${title}**`) + .setDescription(`**${title}** (${duration})`) .setThumbnail(thumbnailUrl) .setFooter({ text: `Requested by ${message.author.username}`, iconURL: message.author.displayAvatarURL() }) .setTimestamp();