-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add proper ratelimiting, and reorganise some things
- Loading branch information
Showing
6 changed files
with
129 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,4 @@ engines: | |
enabled: true | ||
ratings: | ||
paths: | ||
- "lib/**.js" | ||
exclude_paths: | ||
- data/* | ||
- assets/* | ||
- "lib/**.js" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,13 @@ | ||
module.exports = require('./lib/Sagiri'); | ||
const Handler = require('./lib/Handler'); | ||
const Constants = require('./lib/Constants'); | ||
const Ratelimiter = require('./lib/Ratelimiter'); | ||
|
||
function Sagiri(key, options) { | ||
return new Handler(key, options); | ||
} | ||
|
||
Sagiri.Handler = Handler; | ||
Sagiri.Constants = Constants; | ||
Sagiri.Ratelimiter = Ratelimiter; | ||
|
||
module.exports = Sagiri; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Ratelimiter class. | ||
* | ||
* @prop {Number} totalUses Amount of times a entity can be used before being ratelimited. | ||
* @prop {Number} interval Interval between resetting amount of uses. | ||
* @prop {Number} uses Number of current uses this interval has. | ||
*/ | ||
class Ratelimiter { | ||
/** | ||
* Constructs a new ratelimiter. | ||
* | ||
* @param {Number} totalUses The total amount of uses the ratelimiter can be used before | ||
* @param {Number} interval Time in milliseoncds between resettings uses. | ||
*/ | ||
constructor(totalUses, interval) { | ||
if (typeof totalUses !== 'number') throw new TypeError('totalUses is not not a number.'); | ||
if (typeof interval !== 'number') throw new TypeError('interval is not not a number.'); | ||
|
||
this.totalUses = totalUses; | ||
this.interval = interval; | ||
this.uses = 0; | ||
this._timer = setInterval(() => this.uses = 0, interval); | ||
} | ||
|
||
/** | ||
* Add a use to the ratelimiter. | ||
*/ | ||
use() { | ||
if (this.uses < this.totalUses) this.uses++; | ||
} | ||
|
||
get ratelimited() { | ||
return this.uses === this.totalUses; | ||
} | ||
} | ||
|
||
module.exports = Ratelimiter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters