const { MessageEmbed, Collection } = require("discord.js"); const Discord = require("discord.js") /** * * @param {*} message A DiscordMessage, with the client, information * @param {*} command The Command with the command.name * @returns BOOLEAN */ module.exports.onCoolDown = onCoolDown; function onCoolDown(message, cmd) { if(!message || !message.client) throw "No Message with a valid DiscordClient granted as First Parameter"; if(!cmd || !cmd.name) throw "No Command with a valid Name granted as Second Parameter"; const client = message.client; if (!client.cooldowns.has(cmd.name)) { //if its not in the cooldown, set it too there client.cooldowns.set(cmd.name, new Collection()); } const now = Date.now(); //get the current time const timestamps = client.cooldowns.get(cmd.name); //get the timestamp of the last used commands const cooldownAmount = (cmd.cooldown || settings.default_cooldown_in_sec) * 1000; //get the cooldownamount of the command, if there is no cooldown there will be automatically 1 sec cooldown, so you cannot spam it^^ if (timestamps.has(message.member.id)) { //if the user is on cooldown const expirationTime = timestamps.get(message.member.id) + cooldownAmount; //get the amount of time the user needs to wait until he can run the cmd again if (now < expirationTime) { //if the user is still on cooldonw const timeLeft = (expirationTime - now) / 1000; //get the lefttime //return true return timeLeft } else { //if user is not on cooldown, set it to the cooldown timestamps.set(message.member.id, now); //set a timeout function with the cooldown, so it gets deleted later on again setTimeout(() => timestamps.delete(message.member.id), cooldownAmount); //return false aka not on cooldown return false; } } else { //if user is not on cooldown, set it to the cooldown timestamps.set(message.member.id, now); //set a timeout function with the cooldown, so it gets deleted later on again setTimeout(() => timestamps.delete(message.member.id), cooldownAmount); //return false aka not on cooldown return false; } }