How do I make these commands a 1 day cooldown and a 3 week coldown

1st command:

if (talkedRecently.has(message.author.id)) {
message.reply(You need to wait ${1} day to use this command again!).then(m => {
setTimeout(() => {
m.delete(m)
}, 5000); //5 seconds
2nd command:

if (talkedRecently.has(message.author.id)) {
    message.reply(`You need to wait ${3} weeks to use this command again!`).then(m => {
        setTimeout(() => {
            m.delete(m)
        }, 5000); //5 seconds

is it already fixed or?

Here is a pretty simple way to do it, using quick.db and ms

let ms = require('ms')
let db = require('quick.db')
//check if there is cooldown
	const timeout = 604800000; // 7 days in milliseconds, change to the desired cooldown time, in milliseconds
	const cooldown = await db.fetch(`cooldown_Command-Name_${message.guild.id}_${message.author.id}`);

	if (cooldown !== null && timeout - (Date.now() - cooldown) > 0) {
		const time = ms(timeout - (Date.now() - cooldown));
		message.channel.send(`Sorry you must wait **${time.days}d ${time.hours}h ${time.minutes}m ${time.seconds}s** before using this command again!`);
	} else {
//code here that runs if there is no cooldown

// code put after no cooldown code, to set the cooldown
		db.set(`cooldown_Command-Name_${message.guild.id}_${message.author.id}`, Date.now());
}

Make sure to replace “Command-Name” in db.fetch and db.set, to the name of the command, so it is stored in a seperate database and does not mess up any other cooldown.

1 Like

The cooldown works perfectly, but for some reason time.days, time.hours, time.minutes, time.seconds is undefined. What could be the issue with this?

Please make a new post in #discord-help

Posted: https://support.glitch.com/t/discord-js-variable-undefined-not-sure-why/38060

1 Like