This repository has been archived by the owner on Jun 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcooldown.ts
150 lines (126 loc) · 3.27 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
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import * as Discord from "discord.js";
function now() {
return Math.floor(new Date().getTime() / 1000);
}
export class Cooldown {
public tokens: number;
public window = 0;
public last = 0;
constructor(
public rate: number,
public per: number,
public type: BucketType,
) {
this.tokens = this.rate;
}
getTokens(current: number = now()) {
let tokens = this.tokens;
if (current > this.window + this.per) {
tokens = this.rate;
}
return tokens;
}
updateRateLimit() {
const current = now();
this.last = current;
this.tokens = this.getTokens(current);
// first token used means that we start a new rate limit window
if (this.tokens == this.rate) {
this.window = current;
}
// check if we are rate limited
if (this.tokens == 0) {
return this.per - (current - this.window);
}
// we're not so decrement our tokens
this.tokens -= 1;
// see if we got rate limited due to this token change, and if
// so update the window to point to our current time frame
if (this.tokens == 0) {
this.window = current;
}
}
reset() {
this.tokens = this.rate;
this.last = 0;
}
clone() {
return new Cooldown(this.rate, this.per, this.type);
}
toString() {
return `<Cooldown rate: ${this.rate} per: ${this.per} window: ${this.window} tokens: ${this.tokens}>`;
}
}
export class CooldownMapping {
private cache: { [key: string]: Cooldown } = {};
constructor(public cooldown: Cooldown) { }
clone() {
const ret = new CooldownMapping(this.cooldown);
ret.cache = Object.assign({}, this.cache);
return ret;
}
get valid() {
return this.cooldown !== null;
}
static fromCooldown(rate: number, per: number, type: BucketType) {
return new CooldownMapping(new Cooldown(rate, per, type));
}
private bucketKey(message: Discord.Message) {
const bucketType = this.cooldown.type;
switch (bucketType) {
case BucketType.user:
return message.author.id;
case BucketType.guild:
return (message.guild || message.author).id;
case BucketType.channel:
return message.channel.id;
case BucketType.member:
return `${message.guild && message.guild.id},${message.author.id}`;
case BucketType.category:
return ((message.channel as Discord.GuildChannel).parent || message.channel).id;
}
return "default";
}
private verifyCacheIntegrity() {
// we want to delete all cache objects that haven't been used
// in a cooldown window.e.g.if we have a command that has a
// cooldown of 60s and it has not been used in 60s then that key should be deleted
const current = now();
const deadKeys: string[] = [];
for (const key in this.cache) {
if (this.cache.hasOwnProperty(key)) {
const value = this.cache[key];
if (current > value.last + value.per) {
deadKeys.push(key);
}
}
}
deadKeys.forEach(key => {
delete this.cache[key];
});
}
getBucket(message: Discord.Message) {
if (this.cooldown.type == BucketType.default) {
return this.cooldown;
}
this.verifyCacheIntegrity();
const key = this.bucketKey(message);
let bucket: Cooldown;
if (!this.cache[key]) {
bucket = this.cooldown.clone();
this.cache[key] = bucket;
}
else {
bucket = this.cache[key];
}
return bucket;
}
}
export enum BucketType {
default = 0,
user = 1,
guild = 2,
channel = 3,
member = 4,
category = 5,
}