-
Notifications
You must be signed in to change notification settings - Fork 0
/
cooldownSystem.js
54 lines (46 loc) · 2.07 KB
/
cooldownSystem.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const Discord = require("discord.js");
const { Log } = require("./logSystem");
const cooldowns = new Map();
module.exports = {
async calculateCooldown(command, messageOrInteraction) {
return new Promise((resolve, reject) => {
if (!cooldowns.has(command.name)) cooldowns.set(command.name, new Discord.Collection()); //Add command to cooldown map if not present
const timeStamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown) * 1000;
if (timeStamps.has(messageOrInteraction.member.id)) { //If user has cooldown
const expirationTime = timeStamps.get(messageOrInteraction.member.id) + cooldownAmount;
if (Date.now() < expirationTime) {
let message;
const intervalId = setInterval(async () => {
let timeLeft = (expirationTime - Date.now()) / 1000;
if (Date.now() >= expirationTime) {
if (message) {
if (message.editedTimestamp) {
message.createdTimestamp = message.editedTimestamp;
}
message.delete();
}
clearInterval(intervalId);
resolve(message);
}
let secondOrSeconds = "seconds";
if (timeLeft.toFixed(0) == 1) {
secondOrSeconds = "second";
}
if (!message && Date.now() < expirationTime) {
message = await Log.dcReply(messageOrInteraction, { content: `You're on cooldown for the \`${command.name}\` command. Please wait for \`${timeLeft.toFixed(0)}\` ${secondOrSeconds}!`, ephemeral:false, fetchReply:true });
} else if (Date.now() < expirationTime) {
message = await message.edit({ content: `You're on cooldown for the \`${command.name}\` command. Please wait for \`${timeLeft.toFixed(0)}\` ${secondOrSeconds}!`, ephemeral:false, fetchReply:true });
}
}, 1000)
}
} else { //User does not have cooldown
timeStamps.set(messageOrInteraction.member.id, Date.now());
setTimeout(() => {
timeStamps.delete(messageOrInteraction.member.id) //Delete cooldown
}, cooldownAmount);
resolve(messageOrInteraction);
}
});
}
}