0

I want the bot to send the total time, counting from where it started to where it stopped, I want it to look like this:

hours: h minutes: m seconds: s

const Discord = require('discord.js')
const moment = require('moment')

module.exports = {
    name: "baterponto",
    category: "Mod",
    description: "Bata o seu ponto.",
    run: async (client, interaction) => {

        let terminar = new Discord.MessageActionRow().addComponents(
            new Discord.MessageButton()
                .setCustomId("terminar")
                .setLabel("Finalizar")
                .setStyle(4)
        )

        let p = interaction.user.username;

        let embed = new Discord.MessageEmbed()
            .setAuthor({ name: 'Ponto iniciado' })
            .addFields({ name: 'Usuario:', value: `> ${p}`})
            .addFields({ name: 'Iniciou:', value: `<t:${moment(interaction.creationTimestamp).unix()}>`})
            .addFields({ name: `Finalizou:`, value: `> ...`})

        const msg = await interaction.channel.send({embeds: [embed], components: [terminar]})
        const collector = msg.createMessageComponentCollector()

        collector.on('collect', async(collected) => {

            if(collected.user.id != interaction.user.id) return collected.reply(
                { content: `:x: \`|\` **Somente a pessoa que executou o comando (\`${interaction.user.tag}\`) pode interagir com ele.**`, ephemeral: true }
            );

            if( collected.customId === "terminar" ) {
                const terminou = new Discord.MessageEmbed()
                    .setAuthor({name:"Ponto encerrado"})
                    .addFields({name:`Usuario:`, value: `${p}`})
                    .addFields({name: `Iniciou:`, value:  `<t:${moment(interaction.createdTimestamp).unix()}>`})
                    .addFields({name:`Finalizou:`, value: `<t:${moment(collected.createdTimestamp).unix()}>`})
    
                msg.edit({ 
                    embeds: [terminou],
                    components: []
                })
            }
        })
    }
}
Christian
  • 4,902
  • 4
  • 24
  • 42
  • the bot shows the time it started counting and when it stopped, I want it to show the time it was counting example: 1 minute – Kauã4op Aug 17 '22 at 02:24

1 Answers1

0

Use client.uptime for the milliseconds the bot is up. Something like this should work:

const Discord = require('discord.js')
const moment = require('moment')

module.exports = {
    name: "baterponto",
    category: "Mod",
    description: "Bata o seu ponto.",
    run: async (client, interaction) => {

        let terminar = new Discord.MessageActionRow().addComponents(
            new Discord.MessageButton()
                .setCustomId("terminar")
                .setLabel("Finalizar")
                .setStyle(4)
        )

        let p = interaction.user.username;

        let embed = new Discord.MessageEmbed()
            .setAuthor({ name: 'Ponto iniciado' })
            .addFields({ name: 'Usuario:', value: `> ${p}`})
            .addFields({ name: 'Iniciou:', value: `<t:${moment(interaction.creationTimestamp).unix()}>`})
            .addFields({ name: `Finalizou:`, value: `> ...`})

        const msg = await interaction.channel.send({embeds: [embed], components: [terminar]})
        const collector = msg.createMessageComponentCollector()

        collector.on('collect', async(collected) => {

            if(collected.user.id != interaction.user.id) return collected.reply(
                { content: `:x: \`|\` **Somente a pessoa que executou o comando (\`${interaction.user.tag}\`) pode interagir com ele.**`, ephemeral: true }
            );

            if( collected.customId === "terminar" ) {
                const terminou = new Discord.MessageEmbed()
                    .setAuthor({name:"Ponto encerrado"})
                    .addFields({name:`Usuario:`, value: `${p}`})
                    .addFields({name: `Iniciou:`, value:  `<t:${moment(interaction.createdTimestamp).unix()}>`})
                    .addFields({name:`Finalizou:`, value: `<t:${moment(collected.createdTimestamp).unix()}>`})
    
                msg.edit({ 
                    embeds: [terminou],
                    components: []
                })
            }
        })

        // convert to function
        const totalSeconds = (client.uptime / 1000);
        const days = Math.floor(totalSeconds / 86400);
        
        totalSeconds %= 86400;
        const hours = Math.floor(totalSeconds / 3600);
        
        totalSeconds %= 3600;
        const minutes = Math.floor(totalSeconds / 60);
        const seconds = Math.floor(totalSeconds % 60);

        // convert to whatever you want to use
        console.log('hours: ' + `${hours}` + ' minutes: ' + `${minutes}` + ' seconds: ' + `${seconds}`);
        
    }
}

Reference: Get Uptime of Discord.JS bot

Christian
  • 4,902
  • 4
  • 24
  • 42