Home > other >  How to make a relative timestamp using ms (Or any other way), Discord.js
How to make a relative timestamp using ms (Or any other way), Discord.js

Time:08-21

I'm trying to make a revive command, however, I'd like to have a count down on this command, I've tried a lot of ways, but it requires a Unix code, I tried to find a way to turn Milliseconds into Unix Code, but I didn't find anything, my goal is to make it reply with Wasn't on cooldown! when there is no cooldown running, and Cooldown! (Relative Timestamp Going with the cooldown) to make it easier to check if the cooldown is done, rather than having the spam the command on the last seconds. All I need is a way to convert Milliseconds into Unix Code, or any other way to use the databased cooldown to make a relative timestamp.

Code:

/* DisShop Copyrights */

// Main Requirements \\

const { MessageEmbed } = require("discord.js")
const { Secrets, Channels, Guilds, Roles, Permission, Users } = require(`../../config`)


// Important Values \\

const commandInformation = 
{
    CommandName: "revive",
    Alias: [],
    ReqPermissions: [Permission.ADMIN],
    ReqRoles: [Roles.MAIN_DEVT, Roles.MAIN_TESTT],
    ReqUsers: []
}


// Optional Requirements \\

const { QuickDB } = require('quick.db');
const db = new QuickDB();

const ms = require('ms')
const { create } = require('discord-timestamps')

const anyRequiredRoles = commandInformation.ReqRoles.length > 0
const anyRequiredPermissions = commandInformation.ReqPermissions.length > 0
const anyRequiredUsers = commandInformation.ReqUsers.length > 0

const anyRequirements = anyRequiredRoles == false && anyRequiredPermissions == false && anyRequiredUsers == false

// Code \\

module.exports = client => {
    client.on('messageCreate', async message => {
        if (message.author.bot) return
        if (!message.content.toLowerCase().startsWith(Secrets.PREFIX.toLowerCase())) return

        const command = message.content.toLowerCase()
        const args = command.replace(Secrets.PREFIX.toLowerCase(), '').split(' ')

        if (args[0].toString().toLowerCase() == commandInformation.CommandName || commandInformation.Alias.includes(args[0])) {

            // Permit Check \\

            var Permited_2 = 0

            var Permited_1 = 0

            if (anyRequiredUsers) {
                if (commandInformation.ReqUsers.some(userid => message.author.id == userid)) Permited_1  ;
            }

            if (anyRequiredPermissions || anyRequiredRoles) {
                if (anyRequiredPermissions && !Permited_1 >= 1) {
                    if (commandInformation.ReqPermissions.some(permission => message.member.permissions.has(permission))) Permited_2  ;
                }
                if (anyRequiredRoles && Permited_2 <= 0 && !Permited_1 >= 1) {
                    if (commandInformation.ReqRoles.some(role => message.member.roles.cache.has(role))) Permited_2  ;
                }
            }

            if (!Permited_1 >= 1 && !Permited_2 >= 1 && anyRequiredUsers) {
                const promises = commandInformation.ReqUsers.map(
                    async (user) => await message.guild.members.fetch(user),
                );
                const allowedUsersList = await Promise.all(promises);
                const tags = allowedUsersList.map((m) => m.user.tag);

                return message.reply(`You're not allowed to use this command, only \`${tags.join('`, `')}\` can.`)
            } else if (!Permited_1 >= 1 && !Permited_2 >= 1 && !anyRequiredUsers) {
                return message.reply(`Sorry, you don't have the required permissions and/or roles.`)
            }

            if (!Permited_1 >= 1 && !Permited_2 >= 1) return message.reply(`Sorry, you don't have the required permissions and/or roles.`)

            // Main Code \\

            let ReviveCooldown = 1.8e 6; // 30 Minutes in ms

            let cooldownDB = await db.get(`ReviveCooldown`);

            if (ReviveCooldown - (Date.now() - cooldownDB) > 0){
                let timeObj = ms(ReviveCooldown - (Date.now() - cooldownDB));
                console.log(timeObj, cooldownDB, Date.now(), ReviveCooldown - (Date.now() - cooldownDB)/1000)
                if (timeObj.includes('ms')) timeObj = `Less than a second!`
                message.reply(`Cooldown! (Timestamp?)`)
            }
            else { 
                await db.set(`ReviveCooldown`, Date.now());
                message.reply(`Wasn't on cooldown!`)
            }
        }
    })
}

More info:

  "dependencies":
    "better-sqlite3": "^7.6.2",
    "discord.js": "^13.10.2",
    "fs": "^0.0.1-security",
    "mongoose": "^6.5.2",
    "ms": "^2.1.3",
    "quick.db": "^9.0.6"
    
    "node": "16.14.2"

Thank you! (There are no errors)

CodePudding user response:

You just had to define a function that took a timestamp parameter and we had to divide it by 1000 to convert ms to seconds. Let's say you took a current timestamp add 30mins to it and feed it to the function, it will return the discord Embeddable countdown timer for 30mins.

const getCDStamp = (timestamp = Date.now()) => `<t:${Math.round(timestamp / 1000)}:R>`;

Full Code: https://sourceb.in/ki3jQuUXwe

  • Related