mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-14 10:33:30 +01:00
Resilience to abusers of the API
For some reason there's a way to join text channels via the Discord API but not the Discord Client, so this commit prevents the Client from crashing by checking to see if the channels are voice channels.
This commit is contained in:
@@ -1 +1,8 @@
|
||||
"use strict";exports.__esModule = true;exports.reg = reg;function reg(c,a){return [c].concat(Array.prototype.slice.call(a));}
|
||||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
exports.reg = reg;
|
||||
|
||||
function reg(c, a) {
|
||||
return [c].concat(Array.prototype.slice.call(a));
|
||||
}
|
||||
|
||||
@@ -1 +1,109 @@
|
||||
"use strict";exports.__esModule = true;function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}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 discrimS=Symbol();var discrimCacheS=Symbol();var Cache=(function(_Array){_inherits(Cache,_Array);function Cache(discrim,limit){_classCallCheck(this,Cache);_Array.call(this);this[discrimS] = discrim || "id";this[discrimCacheS] = {};}Cache.prototype.get = function get(key,value){if(key === this[discrimS])return this[discrimCacheS][value] || null;for(var _iterator=this,_isArray=Array.isArray(_iterator),_i=0,_iterator=_isArray?_iterator:_iterator[Symbol.iterator]();;) {var _ref;if(_isArray){if(_i >= _iterator.length)break;_ref = _iterator[_i++];}else {_i = _iterator.next();if(_i.done)break;_ref = _i.value;}var item=_ref;if(item[key] == value){return item;}}return null;};Cache.prototype.has = function has(object){return !!this.get(this[discrimS],object[this[discrimS]]);};Cache.prototype.getAll = function getAll(key,value){var found=new Cache(this[discrimS]);this.forEach(function(val,index,array){if(val.hasOwnProperty(key) && val[key] == value){found.push(val);return;}});return found;};Cache.prototype.add = function add(data){var cacheKey=this[discrimS] === "id"?data.id:data[this[discrimS]];if(this[discrimCacheS][cacheKey]){return this[discrimCacheS][cacheKey];}if(this.limit && this.length >= this.limit){this.splice(0,1);}this.push(data);this[discrimCacheS][cacheKey] = data;return data;};Cache.prototype.update = function update(old,data){var item=this.get(this[discrimS],old[this[discrimS]]);if(item){var index=this.indexOf(item);this[index] = data;this[discrimCacheS][data[this[discrimS]]] = this[index];return this[index];}else {return false;}};Cache.prototype.random = function random(){return this[Math.floor(Math.random() * this.length)];};Cache.prototype.remove = function remove(data){delete this[discrimCacheS][data[this[discrimS]]];var index=this.indexOf(data);if(~index){this.splice(index,1);}else {var item=this.get(this[discrimS],data[this[discrimS]]);if(item){this.splice(this.indexOf(item),1);}}return false;};return Cache;})(Array);exports["default"] = Cache;module.exports = exports["default"];
|
||||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
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 discrimS = Symbol();
|
||||
var discrimCacheS = Symbol();
|
||||
|
||||
var Cache = (function (_Array) {
|
||||
_inherits(Cache, _Array);
|
||||
|
||||
function Cache(discrim, limit) {
|
||||
_classCallCheck(this, Cache);
|
||||
|
||||
_Array.call(this);
|
||||
this[discrimS] = discrim || "id";
|
||||
this[discrimCacheS] = {};
|
||||
}
|
||||
|
||||
Cache.prototype.get = function get(key, value) {
|
||||
if (key === this[discrimS]) return this[discrimCacheS][value] || null;
|
||||
|
||||
for (var _iterator = this, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
|
||||
var _ref;
|
||||
|
||||
if (_isArray) {
|
||||
if (_i >= _iterator.length) break;
|
||||
_ref = _iterator[_i++];
|
||||
} else {
|
||||
_i = _iterator.next();
|
||||
if (_i.done) break;
|
||||
_ref = _i.value;
|
||||
}
|
||||
|
||||
var item = _ref;
|
||||
|
||||
if (item[key] == value) {
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
Cache.prototype.has = function has(object) {
|
||||
return !!this.get(this[discrimS], object[this[discrimS]]);
|
||||
};
|
||||
|
||||
Cache.prototype.getAll = function getAll(key, value) {
|
||||
var found = new Cache(this[discrimS]);
|
||||
this.forEach(function (val, index, array) {
|
||||
if (val.hasOwnProperty(key) && val[key] == value) {
|
||||
found.push(val);
|
||||
return;
|
||||
}
|
||||
});
|
||||
return found;
|
||||
};
|
||||
|
||||
Cache.prototype.add = function add(data) {
|
||||
var cacheKey = this[discrimS] === "id" ? data.id : data[this[discrimS]];
|
||||
if (this[discrimCacheS][cacheKey]) {
|
||||
return this[discrimCacheS][cacheKey];
|
||||
}
|
||||
if (this.limit && this.length >= this.limit) {
|
||||
this.splice(0, 1);
|
||||
}
|
||||
this.push(data);
|
||||
this[discrimCacheS][cacheKey] = data;
|
||||
return data;
|
||||
};
|
||||
|
||||
Cache.prototype.update = function update(old, data) {
|
||||
var item = this.get(this[discrimS], old[this[discrimS]]);
|
||||
if (item) {
|
||||
var index = this.indexOf(item);
|
||||
this[index] = data;
|
||||
this[discrimCacheS][data[this[discrimS]]] = this[index];
|
||||
return this[index];
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
Cache.prototype.random = function random() {
|
||||
return this[Math.floor(Math.random() * this.length)];
|
||||
};
|
||||
|
||||
Cache.prototype.remove = function remove(data) {
|
||||
delete this[discrimCacheS][data[this[discrimS]]];
|
||||
var index = this.indexOf(data);
|
||||
if (~index) {
|
||||
this.splice(index, 1);
|
||||
} else {
|
||||
var item = this.get(this[discrimS], data[this[discrimS]]);
|
||||
if (item) {
|
||||
this.splice(this.indexOf(item), 1);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
return Cache;
|
||||
})(Array);
|
||||
|
||||
exports["default"] = Cache;
|
||||
module.exports = exports["default"];
|
||||
|
||||
@@ -8,5 +8,38 @@
|
||||
instances sometimes.
|
||||
|
||||
Instead, use objectThatExtendsEquality.equals()
|
||||
*/"use strict";exports.__esModule = true;var _createClass=(function(){function defineProperties(target,props){for(var i=0;i < props.length;i++) {var descriptor=props[i];descriptor.enumerable = descriptor.enumerable || false;descriptor.configurable = true;if("value" in descriptor)descriptor.writable = true;Object.defineProperty(target,descriptor.key,descriptor);}}return function(Constructor,protoProps,staticProps){if(protoProps)defineProperties(Constructor.prototype,protoProps);if(staticProps)defineProperties(Constructor,staticProps);return Constructor;};})();function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}var Equality=(function(){function Equality(){_classCallCheck(this,Equality);}Equality.prototype.equals = function equals(object){return object && object[this.eqDiscriminator] === this[this.eqDiscriminator];};Equality.prototype.equalsStrict = function equalsStrict(object){ // override per class type
|
||||
return;};_createClass(Equality,[{key:"eqDiscriminator",get:function get(){return "id";}}]);return Equality;})();exports["default"] = Equality;module.exports = exports["default"];
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var Equality = (function () {
|
||||
function Equality() {
|
||||
_classCallCheck(this, Equality);
|
||||
}
|
||||
|
||||
Equality.prototype.equals = function equals(object) {
|
||||
return object && object[this.eqDiscriminator] === this[this.eqDiscriminator];
|
||||
};
|
||||
|
||||
Equality.prototype.equalsStrict = function equalsStrict(object) {
|
||||
// override per class type
|
||||
return;
|
||||
};
|
||||
|
||||
_createClass(Equality, [{
|
||||
key: "eqDiscriminator",
|
||||
get: function get() {
|
||||
return "id";
|
||||
}
|
||||
}]);
|
||||
|
||||
return Equality;
|
||||
})();
|
||||
|
||||
exports["default"] = Equality;
|
||||
module.exports = exports["default"];
|
||||
|
||||
@@ -1,8 +1,153 @@
|
||||
"use strict"; /* global process */exports.__esModule = true;function _interopRequireDefault(obj){return obj && obj.__esModule?obj:{"default":obj};}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}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 _fsExtra=require("fs-extra");var _fsExtra2=_interopRequireDefault(_fsExtra);var _events=require("events");var _events2=_interopRequireDefault(_events);var _crypto=require("crypto");var _crypto2=_interopRequireDefault(_crypto);var savePaths=[process.env.APPDATA || (process.platform == "darwin"?process.env.HOME + "Library/Preference":"/var/local"),process.env[process.platform == "win32"?"USERPROFILE":"HOME"],process.cwd()];var algo="aes-256-ctr";function secureEmail(email,password){return new Buffer(_crypto2["default"].createHash("sha256").update(email + password,"utf8").digest()).toString("hex");}var TokenCacher=(function(_EventEmitter){_inherits(TokenCacher,_EventEmitter);function TokenCacher(client,options){_classCallCheck(this,TokenCacher);_EventEmitter.call(this);this.client = client;this.savePath = null;this.error = false;this.done = false;this.data = {};}TokenCacher.prototype.setToken = function setToken(){var email=arguments.length <= 0 || arguments[0] === undefined?"":arguments[0];var password=arguments.length <= 1 || arguments[1] === undefined?"":arguments[1];var token=arguments.length <= 2 || arguments[2] === undefined?"":arguments[2];email = secureEmail(email,password);var cipher=_crypto2["default"].createCipher(algo,password);var crypted=cipher.update("valid" + token,"utf8","hex");crypted += cipher.final("hex");this.data[email] = crypted;this.save();};TokenCacher.prototype.save = function save(){_fsExtra2["default"].writeJson(this.savePath,this.data);};TokenCacher.prototype.getToken = function getToken(){var email=arguments.length <= 0 || arguments[0] === undefined?"":arguments[0];var password=arguments.length <= 1 || arguments[1] === undefined?"":arguments[1];email = secureEmail(email,password);if(this.data[email]){try{var decipher=_crypto2["default"].createDecipher(algo,password);var dec=decipher.update(this.data[email],"hex","utf8");dec += decipher.final("utf8");return dec.indexOf("valid") === 0?dec.substr(5):false;}catch(e) { // not a valid token
|
||||
return null;}}else {return null;}};TokenCacher.prototype.init = function init(ind){var _this=this;var self=this;var savePath=savePaths[ind];_fsExtra2["default"].ensureDir(savePath,function(err){if(err){error(err);}else { //good to go
|
||||
_fsExtra2["default"].ensureFile(savePath + "/.discordjs/tokens.json",function(err){if(err){error(err);}else { //file exists
|
||||
_fsExtra2["default"].readFile(savePath + "/.discordjs/tokens.json",function(err,data){if(err){error(err);}else { // can read file, is it valid JSON?
|
||||
try{_this.data = JSON.parse(data); // good to go!
|
||||
_this.savePath = savePath + "/.discordjs/tokens.json";_this.emit("ready");_this.done = true;}catch(e) { // not valid JSON, make it valid and then write
|
||||
_fsExtra2["default"].writeJson(savePath + "/.discordjs/tokens.json",{},function(err){if(err){error(err);}else { // good to go!
|
||||
_this.savePath = savePath + "/.discordjs/tokens.json";_this.emit("ready");_this.done = true;}});}}});}});}});function error(e){ind++;if(!savePaths[ind]){self.emit("error");self.error = e;self.done = true;}else {self.init(ind);}}};return TokenCacher;})(_events2["default"]);exports["default"] = TokenCacher;module.exports = exports["default"];
|
||||
"use strict";
|
||||
/* global process */
|
||||
|
||||
exports.__esModule = true;
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
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 _fsExtra = require("fs-extra");
|
||||
|
||||
var _fsExtra2 = _interopRequireDefault(_fsExtra);
|
||||
|
||||
var _events = require("events");
|
||||
|
||||
var _events2 = _interopRequireDefault(_events);
|
||||
|
||||
var _crypto = require("crypto");
|
||||
|
||||
var _crypto2 = _interopRequireDefault(_crypto);
|
||||
|
||||
var savePaths = [process.env.APPDATA || (process.platform == "darwin" ? process.env.HOME + "Library/Preference" : "/var/local"), process.env[process.platform == "win32" ? "USERPROFILE" : "HOME"], process.cwd()];
|
||||
|
||||
var algo = "aes-256-ctr";
|
||||
|
||||
function secureEmail(email, password) {
|
||||
return new Buffer(_crypto2["default"].createHash("sha256").update(email + password, "utf8").digest()).toString("hex");
|
||||
}
|
||||
|
||||
var TokenCacher = (function (_EventEmitter) {
|
||||
_inherits(TokenCacher, _EventEmitter);
|
||||
|
||||
function TokenCacher(client, options) {
|
||||
_classCallCheck(this, TokenCacher);
|
||||
|
||||
_EventEmitter.call(this);
|
||||
this.client = client;
|
||||
this.savePath = null;
|
||||
this.error = false;
|
||||
this.done = false;
|
||||
this.data = {};
|
||||
}
|
||||
|
||||
TokenCacher.prototype.setToken = function setToken() {
|
||||
var email = arguments.length <= 0 || arguments[0] === undefined ? "" : arguments[0];
|
||||
var password = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1];
|
||||
var token = arguments.length <= 2 || arguments[2] === undefined ? "" : arguments[2];
|
||||
|
||||
email = secureEmail(email, password);
|
||||
var cipher = _crypto2["default"].createCipher(algo, password);
|
||||
var crypted = cipher.update("valid" + token, "utf8", "hex");
|
||||
crypted += cipher.final("hex");
|
||||
this.data[email] = crypted;
|
||||
this.save();
|
||||
};
|
||||
|
||||
TokenCacher.prototype.save = function save() {
|
||||
_fsExtra2["default"].writeJson(this.savePath, this.data);
|
||||
};
|
||||
|
||||
TokenCacher.prototype.getToken = function getToken() {
|
||||
var email = arguments.length <= 0 || arguments[0] === undefined ? "" : arguments[0];
|
||||
var password = arguments.length <= 1 || arguments[1] === undefined ? "" : arguments[1];
|
||||
|
||||
email = secureEmail(email, password);
|
||||
|
||||
if (this.data[email]) {
|
||||
|
||||
try {
|
||||
var decipher = _crypto2["default"].createDecipher(algo, password);
|
||||
var dec = decipher.update(this.data[email], "hex", "utf8");
|
||||
dec += decipher.final("utf8");
|
||||
return dec.indexOf("valid") === 0 ? dec.substr(5) : false;
|
||||
} catch (e) {
|
||||
// not a valid token
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
TokenCacher.prototype.init = function init(ind) {
|
||||
var _this = this;
|
||||
|
||||
var self = this;
|
||||
var savePath = savePaths[ind];
|
||||
|
||||
_fsExtra2["default"].ensureDir(savePath, function (err) {
|
||||
|
||||
if (err) {
|
||||
error(err);
|
||||
} else {
|
||||
//good to go
|
||||
|
||||
_fsExtra2["default"].ensureFile(savePath + "/.discordjs/tokens.json", function (err) {
|
||||
if (err) {
|
||||
error(err);
|
||||
} else {
|
||||
//file exists
|
||||
|
||||
_fsExtra2["default"].readFile(savePath + "/.discordjs/tokens.json", function (err, data) {
|
||||
|
||||
if (err) {
|
||||
error(err);
|
||||
} else {
|
||||
// can read file, is it valid JSON?
|
||||
try {
|
||||
|
||||
_this.data = JSON.parse(data);
|
||||
// good to go!
|
||||
_this.savePath = savePath + "/.discordjs/tokens.json";
|
||||
_this.emit("ready");
|
||||
_this.done = true;
|
||||
} catch (e) {
|
||||
// not valid JSON, make it valid and then write
|
||||
_fsExtra2["default"].writeJson(savePath + "/.discordjs/tokens.json", {}, function (err) {
|
||||
if (err) {
|
||||
error(err);
|
||||
} else {
|
||||
// good to go!
|
||||
_this.savePath = savePath + "/.discordjs/tokens.json";
|
||||
_this.emit("ready");
|
||||
_this.done = true;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function error(e) {
|
||||
ind++;
|
||||
if (!savePaths[ind]) {
|
||||
self.emit("error");
|
||||
self.error = e;
|
||||
self.done = true;
|
||||
} else {
|
||||
self.init(ind);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return TokenCacher;
|
||||
})(_events2["default"]);
|
||||
|
||||
exports["default"] = TokenCacher;
|
||||
module.exports = exports["default"];
|
||||
|
||||
Reference in New Issue
Block a user