discord-bot-cli
Version:
An easy way to build a command-based discord bot with discord.js.
48 lines (47 loc) • 1.49 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Throttler = void 0;
const timeout_1 = require("../utils/timeout");
class Throttler {
/**
* @param count How many time the throttler can be triggered.
* @param duration Time in seconds since the first trigger before the throttler is reset.
*/
constructor(count, duration) {
this.count = count;
this.duration = duration;
this._current = 0;
this._timeout = undefined;
}
/** The number of time this throttler has been triggered. */
get current() {
return this._current;
}
/** Either or not this throttler has reached the limit. */
get throttled() {
return this._current >= this.count;
}
/** The time in seconds until the throttler is reset. */
get cooldown() {
return this._timeout ? Math.ceil(timeout_1.getTimeLeft(this._timeout)) : 0;
}
/** Resets this throttler. */
reset() {
if (this._timeout)
clearTimeout(this._timeout);
this._timeout = undefined;
this._current = 0;
}
/**
* Increment the counter.
* @returns Either or not the limit has been reached.
*/
add() {
const reachedLimit = this.throttled;
this._current++;
if (!this._timeout)
this._timeout = setTimeout(() => this.reset(), this.duration * 1000);
return reachedLimit;
}
}
exports.Throttler = Throttler;