discord.js-rate-limiter
Version:
Rate limiter for discord.js.
44 lines • 1.51 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RateLimiter = void 0;
var limiter_1 = require("limiter");
var RateLimiter = /** @class */ (function () {
/**
* Creates a new RateLimiter object to control rate limiting.
*
* @param amount - Amount of times an action can be done within an interval. Ex: `2` would mean 2 times.
* @param interval - Length of an interval in milliseconds. Ex: `5000` would mean 5 seconds.
*
* @returns RateLimiter object.
*/
function RateLimiter(amount, interval) {
this.amount = amount;
this.interval = interval;
this.limiters = {};
}
/**
* Takes a token from the rate limiter.
*
* @param key Key which identifies the entity being limited (Ex: a username or ID).
*
* @returns Whether this action exceeds the rate limit.
*/
RateLimiter.prototype.take = function (key) {
var limiter = this.limiters[key];
if (!limiter) {
limiter = new limiter_1.RateLimiter({
tokensPerInterval: this.amount,
interval: this.interval,
});
this.limiters[key] = limiter;
}
if (limiter.getTokensRemaining() < 1) {
return true;
}
limiter.removeTokens(1);
return false;
};
return RateLimiter;
}());
exports.RateLimiter = RateLimiter;
//# sourceMappingURL=rate-limiter.js.map