mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
Experimental active ratelimits (adapted from Eris)
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -15,6 +15,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
||||
|
||||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
|
||||
|
||||
var _UtilBucket = require("../Util/Bucket");
|
||||
|
||||
var _UtilBucket2 = _interopRequireDefault(_UtilBucket);
|
||||
|
||||
var _UtilEquality = require("../Util/Equality");
|
||||
|
||||
var _UtilEquality2 = _interopRequireDefault(_UtilEquality);
|
||||
@@ -55,13 +59,21 @@ var Server = (function (_Equality) {
|
||||
|
||||
_Equality.call(this);
|
||||
|
||||
var self = this;
|
||||
this.client = client;
|
||||
this.id = data.id;
|
||||
|
||||
if (data.owner_id) {
|
||||
// new server data
|
||||
client.internal.buckets["bot:msg:guild:" + this.id] = new _UtilBucket2["default"](5, 5000);
|
||||
client.internal.buckets["dmsg:" + this.id] = new _UtilBucket2["default"](5, 1000);
|
||||
client.internal.buckets["bdmsg:" + this.id] = new _UtilBucket2["default"](1, 1000);
|
||||
client.internal.buckets["guild_member:" + this.id] = new _UtilBucket2["default"](10, 10000);
|
||||
client.internal.buckets["guild_member_nick:" + this.id] = new _UtilBucket2["default"](1, 1000);
|
||||
}
|
||||
|
||||
this.region = data.region;
|
||||
this.ownerID = data.owner_id || data.ownerID;
|
||||
this.name = data.name;
|
||||
this.id = data.id;
|
||||
this.members = new _UtilCache2["default"]();
|
||||
this.channels = new _UtilCache2["default"]();
|
||||
this.roles = new _UtilCache2["default"]();
|
||||
@@ -72,8 +84,6 @@ var Server = (function (_Equality) {
|
||||
this.memberCount = data.member_count || data.memberCount;
|
||||
this.large = data.large || this.memberCount > 250;
|
||||
|
||||
var self = this;
|
||||
|
||||
if (data.roles instanceof _UtilCache2["default"]) {
|
||||
data.roles.forEach(function (role) {
|
||||
return _this.roles.add(role);
|
||||
|
||||
55
lib/Util/Bucket.js
Normal file
55
lib/Util/Bucket.js
Normal file
@@ -0,0 +1,55 @@
|
||||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var Bucket = (function () {
|
||||
// Adapted from Eris
|
||||
|
||||
function Bucket(tokenLimit, interval) {
|
||||
_classCallCheck(this, Bucket);
|
||||
|
||||
this.tokenLimit = tokenLimit;
|
||||
this.interval = interval;
|
||||
this.extraTime = 500;
|
||||
this.lastReset = this.tokens = this.lastSend = 0;
|
||||
this._queue = [];
|
||||
}
|
||||
|
||||
Bucket.prototype.queue = function queue(func) {
|
||||
this._queue.push(func);
|
||||
this.check();
|
||||
};
|
||||
|
||||
Bucket.prototype.check = function check() {
|
||||
var _this = this;
|
||||
|
||||
if (this.timeout || this._queue.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (this.lastReset + this.interval + this.extraTime < Date.now()) {
|
||||
this.lastReset = Date.now();
|
||||
this.tokens = Math.max(0, this.tokens - this.tokenLimit);
|
||||
}
|
||||
|
||||
var val;
|
||||
while (this._queue.length > 0 && this.tokens < this.tokenLimit) {
|
||||
this.tokens++;
|
||||
this._queue.shift()();
|
||||
this.lastSend = Date.now();
|
||||
}
|
||||
|
||||
if (this._queue.length > 0 && !this.timeout) {
|
||||
this.timeout = setTimeout(function () {
|
||||
_this.timeout = null;
|
||||
_this.check();
|
||||
}, this.tokens < this.tokenLimit ? 1 : Math.max(0, this.lastReset + this.interval + this.extraTime - Date.now()));
|
||||
}
|
||||
};
|
||||
|
||||
return Bucket;
|
||||
})();
|
||||
|
||||
exports["default"] = Bucket;
|
||||
module.exports = exports["default"];
|
||||
Reference in New Issue
Block a user