This repository has been archived by the owner on Feb 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cooldowns.js
63 lines (53 loc) · 1.72 KB
/
cooldowns.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
55
56
57
58
59
60
61
62
63
/* eslint-disable consistent-return */
/*
* This library is used to keep track of cooldowns.
* It uses a custom set implementation.
* There's probably a better way to do this... lol.
*/
const { COOLDOWNS, ADMINS } = require('./config.json');
/**
* A custom class to store cooldowns in.
* Stored in a tuple-esque format [user-id, command-name], but it's a string.
* Thanks to https://stackoverflow.com/a/62076901
*/
class Cooldowns extends Set {
add(arr) {
super.add(arr.toString());
}
has(arr) {
return super.has(arr.toString());
}
delete(arr) {
super.delete(arr.toString());
}
}
// Instantiate the cooldowns set.
const talkedRecently = new Cooldowns();
/**
* This function is used to add a user to the cooldowns set.
* Bypasses admins.
* @param {string} discordUserID The user who is trying to use the command
* @param {string} commandName The command which we're checking the cooldown for.
*/
function addCooldown(discordUserID, commandName) {
// Admins bypass cooldowns.
if (COOLDOWNS.commandName === null || ADMINS.includes(discordUserID)) return;
talkedRecently.add([discordUserID, commandName]);
setTimeout(() => {
talkedRecently.delete([discordUserID, commandName]);
}, COOLDOWNS.commandName * 1000);
}
/**
* This function is used to check if a user is a part of the cooldowns set.
* @param {string} discordUserID The user who is trying to use the command
* @param {string} commandName The command which we're checking the cooldown for.
* @returns {bool} `True` if the user is on cooldown, `False` if not.
*/
function hasCooldown(discordUserID, commandName) {
return talkedRecently.has([discordUserID, commandName]);
}
// Exports
module.exports = {
addCooldown,
hasCooldown,
};