0

I saw in some discord servers that they have a detailed cooldown and that they can exactly see how long it takes before they can use that command again but I don't know how add this, can someone help me?

I have this now

const talkedRecently = new Set();
if (talkedRecently.has(msg.author.id)) {
  msg.channel.send("Wait 1 minute before getting typing this again. - " + msg.author);
} else {
  talkedRecently.add(msg.author.id);
  setTimeout(() => {
    talkedRecently.delete(msg.author.id);
  }, 60000);
}

but here you can't see exactly how long you have to wait. I want it like this:
like this

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
Kieran Julien
  • 43
  • 1
  • 5
  • Hi and welcome to SO. This is not a place to find someone that writes code for you. Instead try to get something to work first and come back when you have a specific code question. Thanks. – Martin Reiche Oct 06 '18 at 07:37
  • https://stackoverflow.com/questions/3144711/find-the-time-left-in-a-settimeout Maybe you need to customise the setTimeout according to your needs. Have a look at this question. –  Oct 07 '18 at 03:56

2 Answers2

1

If you create an object of cooldowns you can get how much time they would have left by subtracting the date from the cooldown.

Like so:

//Start of code or something
var cooldowns = {}
var minute = 60000;
var hour = minute * 24;

//Set cooldown
cooldowns[message.author.id] = Date.now() + hour * 24; //Set a 24 hour cooldown

//At command check
if(cooldowns[message.author.id]){
     if(cooldowns[message.author.id] > Date.now()) delete cooldowns[message.author.id];
     else console.log("user still has " + Math.round((cooldowns[message.author.id] - Date.now)/minute) + " minutes left"
}
  • Where does I have to add this in the code and how do I add the cooldown to a member – Kieran Julien Oct 15 '18 at 06:17
  • read the code, it shows you everything. At the top it shows you variables needed. In the middle it shows you how to "set" a cooldown. So you can put this in a specific command or set it every time a message is received. It's all your choice. At the bottom is code how to check if they still have a cooldown or not. – Frustrated programmer Oct 16 '18 at 18:22
0

I haven't tested it, but the simplest way to do this is something like:

const talkedRecently = new Set();
const seconds = "60";

loopnum = 0
while (loopnum <= seconds)) {
  loopnum = loopnum + 1;
  if (talkedRecently.has(msg.author.id + "-" + loopnum)) {
    msg.channel.send("Wait **" + loopnum + " **seconds before getting typing this again. - " + msg.author);
    return;
  }
}
loopnum = seconds;
while (loopnum = 0) {
  setTimeout(() => {
    if (talkedRecently.has(msg.author.id + "-" + (loopnum+1))) {
      talkedRecently.delete(msg.author.id "-" + (loopnum+1));
    }
    talkedRecently.add(msg.author.id + "-" + loopnum);
  }, 1000);
  loopnum = loopnum - 1;
}
if (talkedRecently.has(msg.author.id + "-1")) {
  talkedRecently.delete(msg.author.id "-1");
}
return;