-
Notifications
You must be signed in to change notification settings - Fork 13
/
RateLimiter.ts
67 lines (59 loc) · 2.51 KB
/
RateLimiter.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
import { Collection, User } from 'discord.js';
import { Message } from '../types/Message';
import { RateLimit } from './RateLimit';
import { Util } from '../util/Util';
import { deprecatedClass } from '../util/DeprecatedClassDecorator';
/**
* Handles assigning ratelimits to guildmembers and users
* @deprecated Will be removed in a future release. Use {@link RateLimitManager} instead
* @param {string} limit Ratelimit string matching the regex `\d+\/\d+[s|m|h|d]`<br>
* **Example:** `1/10m` to limit a command to one use per 10 minutes
* @param {boolean} global Whether or not this RateLimiter handles global ratelimits
*/
@deprecatedClass('Class `RateLimiter` is deprecated. Use `RateLimitManager` instead')
export class RateLimiter
{
private readonly _limit: [number, number];
private readonly _global: boolean;
private readonly _rateLimits: Collection<string, Collection<string, RateLimit>>;
private readonly _globalLimits: Collection<string, RateLimit>;
public constructor(limit: string, global: boolean)
{
this._limit = Util.parseRateLimit(limit);
this._global = global;
this._rateLimits = new Collection<string, Collection<string, RateLimit>>();
this._globalLimits = new Collection<string, RateLimit>();
}
/**
* Returns the RateLimit object for the message author if global
* or message member if message is in a guild. If a userOverride
* is given then the RateLimit or global RateLimit will be
* retrieved for that user based on the message location
* @param {external:Message} message Discord.js Message object
* @param {external:User} userOverride User object to use in place of Message author
* @returns {RateLimit}
*/
public get(message: Message, userOverride?: User): RateLimit
{
const user: User = userOverride ? userOverride : message.author;
if (this._isGlobal(message))
{
if (!this._globalLimits.has(user.id))
this._globalLimits.set(user.id, new RateLimit(this._limit));
return this._globalLimits.get(user.id)!;
}
if (!this._rateLimits.has(message.guild.id))
this._rateLimits.set(message.guild.id, new Collection<string, RateLimit>());
if (!this._rateLimits.get(message.guild.id)!.has(user.id))
this._rateLimits.get(message.guild.id)!.set(user.id, new RateLimit(this._limit));
return this._rateLimits.get(message.guild.id)!.get(user.id)!;
}
/**
* Determine whether or not to use the global rate limit collection
* @private
*/
private _isGlobal(message?: Message): boolean
{
return message ? message.channel.type === 'dm' || this._global : this._global;
}
}