@gruvjs/cooldown 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +67 -0
- package/package.json +37 -0
- package/src/core/CooldownManager.js +110 -0
- package/src/index.js +4 -0
- package/src/util/formatMs.js +22 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Saiteja Madha
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
<br />
|
|
3
|
+
<p>
|
|
4
|
+
<a href="https://github.com/gruvjs/gruvjs"><img src="https://raw.githubusercontent.com/gruvjs/gruvjs/main/assets/logo.svg" width="546" alt="gruvjs" /></a>
|
|
5
|
+
</p>
|
|
6
|
+
<br />
|
|
7
|
+
<p>
|
|
8
|
+
<a href="https://www.npmjs.com/package/@gruvjs/cooldown"><img src="https://img.shields.io/npm/v/@gruvjs/cooldown.svg?maxAge=3600" alt="npm version" /></a>
|
|
9
|
+
<a href="https://www.npmjs.com/package/@gruvjs/cooldown"><img src="https://img.shields.io/npm/dt/@gruvjs/cooldown.svg?maxAge=3600" alt="npm downloads" /></a>
|
|
10
|
+
</p>
|
|
11
|
+
</div>
|
|
12
|
+
|
|
13
|
+
## About
|
|
14
|
+
|
|
15
|
+
`@gruvjs/cooldown` is a per-user, per-guild, per-command cooldown manager for bots. Supports bypass lists and per-check duration overrides.
|
|
16
|
+
|
|
17
|
+
Node.js 18.0.0 or newer is required.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install @gruvjs/cooldown
|
|
23
|
+
yarn add @gruvjs/cooldown
|
|
24
|
+
pnpm add @gruvjs/cooldown
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Examples
|
|
28
|
+
|
|
29
|
+
```js
|
|
30
|
+
const { CooldownManager } = require("@gruvjs/cooldown");
|
|
31
|
+
|
|
32
|
+
const cooldowns = new CooldownManager({
|
|
33
|
+
default: 3000, // 3s default for all commands
|
|
34
|
+
bypass: ["OWNER_ID"], // these users always bypass cooldowns
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// In your command handler:
|
|
38
|
+
const result = cooldowns.check("ping", interaction.user.id);
|
|
39
|
+
|
|
40
|
+
if (!result.ok) {
|
|
41
|
+
return interaction.reply(`Please wait ${result.remainingText} before using this command again.`);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// execute command...
|
|
45
|
+
|
|
46
|
+
// Per-command duration override
|
|
47
|
+
cooldowns.check("ban", userId, { duration: 10_000 }); // 10s just for /ban
|
|
48
|
+
|
|
49
|
+
// Reset manually (e.g. after a premium bypass)
|
|
50
|
+
cooldowns.reset("ping", userId);
|
|
51
|
+
cooldowns.resetCommand("ping"); // reset for all users
|
|
52
|
+
|
|
53
|
+
// Bypass list management
|
|
54
|
+
cooldowns.addBypass("MOD_ID");
|
|
55
|
+
cooldowns.removeBypass("MOD_ID");
|
|
56
|
+
|
|
57
|
+
// Stats
|
|
58
|
+
cooldowns.stats(); // → { active: 5, bypassed: 2 }
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
See the [docs](./docs) folder for detailed usage.
|
|
62
|
+
|
|
63
|
+
## Links
|
|
64
|
+
|
|
65
|
+
- [npm](https://www.npmjs.com/package/@gruvjs/cooldown)
|
|
66
|
+
- [GitHub](https://github.com/gruvjs/gruvjs/tree/main/packages/cooldown)
|
|
67
|
+
- [gruvjs monorepo](https://github.com/gruvjs/gruvjs)
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@gruvjs/cooldown",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Per-user, per-guild, per-command cooldown manager for bots. Supports bypass lists and duration overrides.",
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"author": "gruvjs",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"engines": {
|
|
9
|
+
"node": ">=18.0.0"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"cooldown",
|
|
13
|
+
"bot",
|
|
14
|
+
"discord",
|
|
15
|
+
"rate-limit",
|
|
16
|
+
"command",
|
|
17
|
+
"zero-dependency"
|
|
18
|
+
],
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/gruvjs/gruvjs.git",
|
|
22
|
+
"directory": "packages/cooldown"
|
|
23
|
+
},
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/gruvjs/gruvjs/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/gruvjs/gruvjs/tree/main/packages/cooldown#readme",
|
|
28
|
+
"files": [
|
|
29
|
+
"src/",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"dependencies": {},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "node tests/run.js"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
const { formatMs } = require("../util/formatMs");
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-user, per-guild, per-command cooldown manager.
|
|
5
|
+
*
|
|
6
|
+
* Unlike a generic rate limiter, CooldownManager is designed for bot commands:
|
|
7
|
+
* it tracks per-key cooldowns, supports bypass lists (owner/mods) and
|
|
8
|
+
* per-check duration overrides.
|
|
9
|
+
*/
|
|
10
|
+
class CooldownManager {
|
|
11
|
+
/**
|
|
12
|
+
* @param {object} [options]
|
|
13
|
+
* @param {number} [options.default=3000] Default cooldown in ms.
|
|
14
|
+
* @param {string[]} [options.bypass=[]] IDs that always bypass cooldowns.
|
|
15
|
+
* @param {number} [options.sweepEveryMs=0] Auto-sweep interval. 0 = disabled.
|
|
16
|
+
*/
|
|
17
|
+
constructor(options = {}) {
|
|
18
|
+
this._default = options.default ?? 3000;
|
|
19
|
+
this._bypass = new Set(options.bypass ?? []);
|
|
20
|
+
this._cooldowns = new Map(); // `${command}:${key}` → expiresAt (ms)
|
|
21
|
+
|
|
22
|
+
this._sweepTimer = null;
|
|
23
|
+
if (options.sweepEveryMs > 0) {
|
|
24
|
+
this._sweepTimer = setInterval(() => this._sweep(), options.sweepEveryMs);
|
|
25
|
+
if (this._sweepTimer.unref) this._sweepTimer.unref();
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// ── Core ───────────────────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Checks and applies a cooldown for a command + key combination.
|
|
33
|
+
*
|
|
34
|
+
* @param {string} command
|
|
35
|
+
* @param {string} key User ID, guild ID, or any string identifier
|
|
36
|
+
* @param {object} [options]
|
|
37
|
+
* @param {number} [options.duration] Override duration for this check
|
|
38
|
+
* @returns {{ ok: boolean, remaining: number, remainingText: string, expiresAt: number }}
|
|
39
|
+
*/
|
|
40
|
+
check(command, key, options = {}) {
|
|
41
|
+
if (this._bypass.has(key)) return { ok: true, remaining: 0, remainingText: "0s", expiresAt: 0 };
|
|
42
|
+
|
|
43
|
+
const mapKey = `${command}:${key}`;
|
|
44
|
+
const now = Date.now();
|
|
45
|
+
const expiresAt = this._cooldowns.get(mapKey) ?? 0;
|
|
46
|
+
|
|
47
|
+
if (expiresAt > now) {
|
|
48
|
+
const remaining = expiresAt - now;
|
|
49
|
+
return { ok: false, remaining, remainingText: formatMs(remaining), expiresAt };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const duration = options.duration ?? this._default;
|
|
53
|
+
this._cooldowns.set(mapKey, now + duration);
|
|
54
|
+
return { ok: true, remaining: 0, remainingText: "0s", expiresAt: now + duration };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Peeks at a cooldown without applying it.
|
|
59
|
+
* @param {string} command
|
|
60
|
+
* @param {string} key
|
|
61
|
+
*/
|
|
62
|
+
peek(command, key) {
|
|
63
|
+
if (this._bypass.has(key)) return { ok: true, remaining: 0, remainingText: "0s", expiresAt: 0 };
|
|
64
|
+
const now = Date.now();
|
|
65
|
+
const expiresAt = this._cooldowns.get(`${command}:${key}`) ?? 0;
|
|
66
|
+
const remaining = Math.max(0, expiresAt - now);
|
|
67
|
+
return { ok: remaining === 0, remaining, remainingText: formatMs(remaining), expiresAt };
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Resets the cooldown for a specific command + key. */
|
|
71
|
+
reset(command, key) { this._cooldowns.delete(`${command}:${key}`); }
|
|
72
|
+
|
|
73
|
+
/** Resets all cooldowns for a command across all keys. */
|
|
74
|
+
resetCommand(command) {
|
|
75
|
+
const prefix = `${command}:`;
|
|
76
|
+
for (const k of this._cooldowns.keys()) {
|
|
77
|
+
if (k.startsWith(prefix)) this._cooldowns.delete(k);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Resets all cooldowns. */
|
|
82
|
+
resetAll() { this._cooldowns.clear(); }
|
|
83
|
+
|
|
84
|
+
// ── Bypass ─────────────────────────────────────────────────────────────────
|
|
85
|
+
|
|
86
|
+
addBypass(id) { this._bypass.add(id); return this; }
|
|
87
|
+
removeBypass(id) { this._bypass.delete(id); return this; }
|
|
88
|
+
isBypassed(id) { return this._bypass.has(id); }
|
|
89
|
+
|
|
90
|
+
// ── Stats / lifecycle ──────────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
stats() {
|
|
93
|
+
const now = Date.now();
|
|
94
|
+
let active = 0;
|
|
95
|
+
for (const exp of this._cooldowns.values()) if (exp > now) active++;
|
|
96
|
+
return { active, bypassed: this._bypass.size };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
destroy() {
|
|
100
|
+
if (this._sweepTimer) clearInterval(this._sweepTimer);
|
|
101
|
+
this._cooldowns.clear();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
_sweep() {
|
|
105
|
+
const now = Date.now();
|
|
106
|
+
for (const [k, exp] of this._cooldowns) if (exp <= now) this._cooldowns.delete(k);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
module.exports = { CooldownManager };
|
package/src/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Formats a millisecond duration as a short human-readable string.
|
|
3
|
+
*
|
|
4
|
+
* @param {number} ms
|
|
5
|
+
* @returns {string}
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* formatMs(0) // → "0s"
|
|
9
|
+
* formatMs(500) // → "500ms"
|
|
10
|
+
* formatMs(2500) // → "2.5s"
|
|
11
|
+
* formatMs(90_000) // → "2m"
|
|
12
|
+
* formatMs(3_600_000) // → "1h"
|
|
13
|
+
*/
|
|
14
|
+
function formatMs(ms) {
|
|
15
|
+
if (ms <= 0) return "0s";
|
|
16
|
+
if (ms < 1000) return `${ms}ms`;
|
|
17
|
+
if (ms < 60_000) return `${(ms / 1000).toFixed(1).replace(/\.0$/, "")}s`;
|
|
18
|
+
if (ms < 3_600_000) return `${Math.ceil(ms / 60_000)}m`;
|
|
19
|
+
return `${Math.ceil(ms / 3_600_000)}h`;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { formatMs };
|