mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-12 01:23:31 +01:00
Fixed PermissionOverwrites
This commit is contained in:
86
lib/Structures/PermissionOverwrite.js
Normal file
86
lib/Structures/PermissionOverwrite.js
Normal file
@@ -0,0 +1,86 @@
|
||||
"use strict";
|
||||
|
||||
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 Permissions = require("../Constants.js").Permissions;
|
||||
|
||||
var PermissionOverwrite = (function () {
|
||||
function PermissionOverwrite(data) {
|
||||
_classCallCheck(this, PermissionOverwrite);
|
||||
|
||||
this.id = data.id;
|
||||
this.type = data.type; // member or role
|
||||
this.deny = data.deny;
|
||||
this.allow = data.allow;
|
||||
}
|
||||
|
||||
// returns an array of allowed permissions
|
||||
|
||||
PermissionOverwrite.prototype.setAllowed = function setAllowed(allowedArray) {
|
||||
var _this = this;
|
||||
|
||||
allowedArray.forEach(function (permission) {
|
||||
if (permission instanceof String || typeof permission === "string") {
|
||||
permission = Permissions[permission];
|
||||
}
|
||||
if (permission) {
|
||||
_this.allow |= 1 << permission;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
PermissionOverwrite.prototype.setDenied = function setDenied(deniedArray) {
|
||||
var _this2 = this;
|
||||
|
||||
deniedArray.forEach(function (permission) {
|
||||
if (permission instanceof String || typeof permission === "string") {
|
||||
permission = Permissions[permission];
|
||||
}
|
||||
if (permission) {
|
||||
_this2.deny |= 1 << permission;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
_createClass(PermissionOverwrite, [{
|
||||
key: "allowed",
|
||||
get: function get() {
|
||||
var allowed = [];
|
||||
for (var permName in Permissions) {
|
||||
if (permName === "manageRoles" || permName === "manageChannels") {
|
||||
// these permissions do not exist in overwrites.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!!(this.allow & Permissions[permName])) {
|
||||
allowed.push(permName);
|
||||
}
|
||||
}
|
||||
return allowed;
|
||||
}
|
||||
|
||||
// returns an array of denied permissions
|
||||
}, {
|
||||
key: "denied",
|
||||
get: function get() {
|
||||
var denied = [];
|
||||
for (var permName in Permissions) {
|
||||
if (permName === "manageRoles" || permName === "manageChannels") {
|
||||
// these permissions do not exist in overwrites.
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!!(this.deny & Permissions[permName])) {
|
||||
denied.push(permName);
|
||||
}
|
||||
}
|
||||
return denied;
|
||||
}
|
||||
}]);
|
||||
|
||||
return PermissionOverwrite;
|
||||
})();
|
||||
|
||||
module.exports = PermissionOverwrite;
|
||||
148
lib/Structures/Role.js
Normal file
148
lib/Structures/Role.js
Normal file
@@ -0,0 +1,148 @@
|
||||
"use strict";
|
||||
|
||||
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 Permissions = require("../Constants.js").Permissions;
|
||||
/*
|
||||
|
||||
example data
|
||||
|
||||
{ position: -1,
|
||||
permissions: 36953089,
|
||||
name: '@everyone',
|
||||
managed: false,
|
||||
id: '110007368451915776',
|
||||
hoist: false,
|
||||
color: 0 }
|
||||
*/
|
||||
|
||||
var DefaultRole = [Permissions.createInstantInvite, Permissions.readMessages, Permissions.readMessageHistory, Permissions.sendMessages, Permissions.sendTTSMessages, Permissions.embedLinks, Permissions.attachFiles, Permissions.readMessageHistory, Permissions.mentionEveryone, Permissions.voiceConnect, Permissions.voiceSpeak, Permissions.voiceUseVAD].reduce(function (previous, current) {
|
||||
return previous | current;
|
||||
}, 0);
|
||||
|
||||
var Role = (function () {
|
||||
function Role(data, serverID, client) {
|
||||
_classCallCheck(this, Role);
|
||||
|
||||
this.position = data.position || -1;
|
||||
this.permissions = data.permissions || DefaultRole;
|
||||
this.name = data.name || "@everyone";
|
||||
this.managed = data.managed || false;
|
||||
this.id = data.id;
|
||||
this.hoist = data.hoist || false;
|
||||
this.color = data.color || 0;
|
||||
this.serverID = serverID;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
Role.prototype.serialise = function serialise(explicit) {
|
||||
var _this = this;
|
||||
|
||||
var hp = function hp(perm) {
|
||||
return _this.hasPermission(perm, explicit);
|
||||
};
|
||||
|
||||
return {
|
||||
// general
|
||||
createInstantInvite: hp(Permissions.createInstantInvite),
|
||||
kickMembers: hp(Permissions.kickMembers),
|
||||
banMembers: hp(Permissions.banMembers),
|
||||
manageRoles: hp(Permissions.manageRoles),
|
||||
manageChannels: hp(Permissions.manageChannels),
|
||||
manageServer: hp(Permissions.manageServer),
|
||||
// text
|
||||
readMessages: hp(Permissions.readMessages),
|
||||
sendMessages: hp(Permissions.sendMessages),
|
||||
sendTTSMessages: hp(Permissions.sendTTSMessages),
|
||||
manageMessages: hp(Permissions.manageMessages),
|
||||
embedLinks: hp(Permissions.embedLinks),
|
||||
attachFiles: hp(Permissions.attachFiles),
|
||||
readMessageHistory: hp(Permissions.readMessageHistory),
|
||||
mentionEveryone: hp(Permissions.mentionEveryone),
|
||||
// voice
|
||||
voiceConnect: hp(Permissions.voiceConnect),
|
||||
voiceSpeak: hp(Permissions.voiceSpeak),
|
||||
voiceMuteMembers: hp(Permissions.voiceMuteMembers),
|
||||
voiceDeafenMembers: hp(Permissions.voiceDeafenMembers),
|
||||
voiceMoveMembers: hp(Permissions.voiceMoveMembers),
|
||||
voiceUseVAD: hp(Permissions.voiceUseVAD)
|
||||
};
|
||||
};
|
||||
|
||||
Role.prototype.serialize = function serialize() {
|
||||
// ;n;
|
||||
return this.serialise();
|
||||
};
|
||||
|
||||
Role.prototype.hasPermission = function hasPermission(perm) {
|
||||
var explicit = arguments.length <= 1 || arguments[1] === undefined ? false : arguments[1];
|
||||
|
||||
if (perm instanceof String || typeof perm === "string") {
|
||||
perm = Permissions[perm];
|
||||
}
|
||||
if (!perm) {
|
||||
return false;
|
||||
}
|
||||
if (!explicit) {
|
||||
// implicit permissions allowed
|
||||
if (!!(this.permissions & Permissions.manageRoles)) {
|
||||
// manageRoles allowed, they have all permissions
|
||||
return true;
|
||||
}
|
||||
}
|
||||
// e.g.
|
||||
// !!(36953089 & Permissions.manageRoles) = not allowed to manage roles
|
||||
// !!(36953089 & (1 << 21)) = voice speak allowed
|
||||
|
||||
return !!(this.permissions & perm);
|
||||
};
|
||||
|
||||
Role.prototype.setPermission = function setPermission(permission, value) {
|
||||
if (permission instanceof String || typeof permission === "string") {
|
||||
permission = Permissions[permission];
|
||||
}
|
||||
if (permission) {
|
||||
// valid permission
|
||||
if (value) {
|
||||
this.permissions |= permission;
|
||||
} else {
|
||||
this.permissions &= ~permission;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Role.prototype.setPermissions = function setPermissions(obj) {
|
||||
var _this2 = this;
|
||||
|
||||
obj.forEach(function (value, permission) {
|
||||
if (permission instanceof String || typeof permission === "string") {
|
||||
permission = Permissions[permission];
|
||||
}
|
||||
if (permission) {
|
||||
// valid permission
|
||||
_this2.setPermission(permission, value);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Role.prototype.colorAsHex = function colorAsHex() {
|
||||
var val = this.color.toString();
|
||||
while (val.length < 6) {
|
||||
val = "0" + val;
|
||||
}
|
||||
return "#" + val;
|
||||
};
|
||||
|
||||
_createClass(Role, [{
|
||||
key: "server",
|
||||
get: function get() {
|
||||
return this.client.internal.servers.get("id", this.serverID);
|
||||
}
|
||||
}]);
|
||||
|
||||
return Role;
|
||||
})();
|
||||
|
||||
module.exports = Role;
|
||||
@@ -13,6 +13,7 @@ var User = require("./User.js");
|
||||
var Member = require("./Member.js");
|
||||
var TextChannel = require("./TextChannel.js");
|
||||
var VoiceChannel = require("./VoiceChannel.js");
|
||||
var Role = require("./Role.js");
|
||||
|
||||
var Server = (function (_Equality) {
|
||||
_inherits(Server, _Equality);
|
||||
@@ -44,7 +45,6 @@ var Server = (function (_Equality) {
|
||||
});
|
||||
|
||||
data.channels.forEach(function (dataChannel) {
|
||||
|
||||
if (dataChannel.type === "text") {
|
||||
var channel = client.internal.channels.add(new TextChannel(dataChannel, client));
|
||||
_this.channels.add(channel);
|
||||
@@ -53,6 +53,10 @@ var Server = (function (_Equality) {
|
||||
_this.channels.add(channel);
|
||||
}
|
||||
});
|
||||
|
||||
data.roles.forEach(function (dataRole) {
|
||||
_this.roles.add(new Role(dataRole, _this));
|
||||
});
|
||||
}
|
||||
|
||||
Server.prototype.toString = function toString() {
|
||||
|
||||
9
lib/Structures/ServerRole.js
Normal file
9
lib/Structures/ServerRole.js
Normal file
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
|
||||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||||
|
||||
var ServerRole = function ServerRole() {
|
||||
_classCallCheck(this, ServerRole);
|
||||
};
|
||||
|
||||
module.exports = ServerRole;
|
||||
@@ -1,22 +1,46 @@
|
||||
"use strict";
|
||||
|
||||
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"); } }
|
||||
|
||||
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 Channel = require("./Channel.js");
|
||||
var Cache = require("../Util/Cache.js");
|
||||
var PermissionOverwrite = require("./PermissionOverwrite.js");
|
||||
|
||||
var TextChannel = (function (_Channel) {
|
||||
_inherits(TextChannel, _Channel);
|
||||
|
||||
function TextChannel(data, client) {
|
||||
var _this = this;
|
||||
|
||||
_classCallCheck(this, TextChannel);
|
||||
|
||||
_Channel.call(this, data, client);
|
||||
|
||||
this.name = data.name;
|
||||
this.topic = data.topic;
|
||||
this.position = data.position;
|
||||
this.lastMessageID = data.last_message_id;
|
||||
this.messages = new Cache("id", client.options.maximumMessages);
|
||||
|
||||
this.permissionOverwrites = new Cache();
|
||||
data.permission_overwrites.forEach(function (permission) {
|
||||
_this.permissionOverwrites.add(new PermissionOverwrite(permission));
|
||||
});
|
||||
}
|
||||
|
||||
/* warning! may return null */
|
||||
|
||||
_createClass(TextChannel, [{
|
||||
key: "lastMessage",
|
||||
get: function get() {
|
||||
return this.messages.get("id", this.lastMessageID);
|
||||
}
|
||||
}]);
|
||||
|
||||
return TextChannel;
|
||||
})(Channel);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user