I’m trying to keep this code of mine coming back in real time

Asked

Viewed 36 times

0

I am with a saving command and wanted that when the user, who already used the command, wait 30 minutes but the code returns an error informing remaining time to use the command again as I do ?

-- CODIGO
const cooldown = new Set();
const db = require('quick.db');
const ms = require('ms');

module.exports.run = async (client, message, args) => {
    if(!message.content.startsWith(','))return;  

    const user = message.author;
    const author = await db.fetch(`work_${message.guild.id}_${user.id}`)

    const timeout = 1800000;

    if (author !== null && timeout - (Date.now() - message.author) > 0) {
        const time = ms(timeout - (Date.now() - message.author));
    
        if (cooldown.has(message.author.id)){
        message.channel.send(` ${user} | <a:negado:872682572965118033> Você já trabalhou hoje,Tente denovo em **${timeout} minutos**`)
      } else {

        const replies = ['Programador','Construtor','Bancário','Motorista','Chefe','Mecanico']

        const result = Math.floor((Math.random() * replies.length));
        const amount = Math.floor(Math.random() * 1000) + 1;
        message.channel.send(` ${user} | <a:azul:876222068688052315> Você trabalhou como **${replies[result]}** e Ganhou Salario De **${amount}** `)
        
        db.add(`money_${message.guild.id}_${user.id}`, amount)
        db.set(`work_${message.guild.id}_${user.id}`, Date.now())
        cooldown.add(message.author.id);
        setTimeout(() => {
            cooldown.delete(message.author.id)
        }, `${timeout}`);
    };
}
}
  • 1

    I don’t get it @Danizavtz

  • Oh yes, someone already edited the question, now it is already with correct appearance.

  • Can you help me @Danizavtz

  • I can’t, I don’t understand any of those libs you’re using.

  • is for a bot application within Discord @Danizavtz

  • const time = ms(timeout - (Date.now() - message.author)); This is already confusing, message.author would be a number. But anyway, what I would do is use a Map in place of Set associating the user id and a waiting timestamp, would search for id, check the current timestamp with the user’s limit and return a time difference message if it is positive value.

  • @Cmtecardeal Only Suggests Changing the "Set" to "Map"

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.