-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathCooldown.ts
37 lines (32 loc) · 1.06 KB
/
Cooldown.ts
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
import { Collection } from "@discordjs/collection";
import { decodeJid } from "../Common/Functions";
import { ICtx } from "../Common/Types";
import EventEmitter from "events";
export class Cooldown extends EventEmitter {
ms: number;
cooldown: Collection<unknown, unknown> | undefined;
timeout: number;
constructor(ctx: ICtx, ms: number) {
super();
this.ms = ms;
this.cooldown = ctx._self.cooldown;
this.timeout = 0;
let q = `cooldown_${ctx._used.command}_${decodeJid(ctx._msg.key.remoteJid as string)}_${decodeJid(ctx._sender.jid as string)}`;
const get = this.cooldown?.get(q);
if (get) {
this.timeout = Number(get) - Date.now();
} else {
this.cooldown?.set(q, Date.now() + ms);
setTimeout(() => {
this.cooldown?.delete(q);
this.emit("end");
}, ms);
}
}
get onCooldown(): boolean {
return this.timeout ? true : false;
}
get timeleft(): number {
return this.timeout;
}
}