mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
Overhaul Permissions utilities (EvaluatedPermissions no more)
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
const Constants = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* The final evaluated permissions for a member in a channel
|
||||
*/
|
||||
class EvaluatedPermissions {
|
||||
constructor(member, raw) {
|
||||
/**
|
||||
* The member this permissions refer to
|
||||
* @type {GuildMember}
|
||||
*/
|
||||
this.member = member;
|
||||
|
||||
/**
|
||||
* A number representing the packed permissions
|
||||
* @type {number}
|
||||
*/
|
||||
this.raw = raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an object mapping permission name, e.g. `READ_MESSAGES` to a boolean - whether the user
|
||||
* can perform this or not.
|
||||
* @returns {Object<string, boolean>}
|
||||
*/
|
||||
serialize() {
|
||||
const serializedPermissions = {};
|
||||
for (const permissionName in Constants.PermissionFlags) {
|
||||
serializedPermissions[permissionName] = this.hasPermission(permissionName);
|
||||
}
|
||||
return serializedPermissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the user has a certain permission, e.g. `READ_MESSAGES`.
|
||||
* @param {PermissionResolvable} permission The permission to check for
|
||||
* @param {boolean} [explicit=false] Whether to require the user to explicitly have the exact permission
|
||||
* @returns {boolean}
|
||||
*/
|
||||
hasPermission(permission, explicit = false) {
|
||||
permission = this.member.client.resolver.resolvePermission(permission);
|
||||
if (!explicit && (this.raw & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
|
||||
return (this.raw & permission) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the user has all specified permissions.
|
||||
* @param {PermissionResolvable[]} permissions The permissions to check for
|
||||
* @param {boolean} [explicit=false] Whether to require the user to explicitly have the exact permissions
|
||||
* @returns {boolean}
|
||||
*/
|
||||
hasPermissions(permissions, explicit = false) {
|
||||
return permissions.every(p => this.hasPermission(p, explicit));
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the user has all specified permissions, and lists any missing permissions.
|
||||
* @param {PermissionResolvable[]} permissions The permissions to check for
|
||||
* @param {boolean} [explicit=false] Whether to require the user to explicitly have the exact permissions
|
||||
* @returns {PermissionResolvable[]}
|
||||
*/
|
||||
missingPermissions(permissions, explicit = false) {
|
||||
return permissions.filter(p => !this.hasPermission(p, explicit));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EvaluatedPermissions;
|
||||
@@ -1,8 +1,7 @@
|
||||
const Channel = require('./Channel');
|
||||
const Role = require('./Role');
|
||||
const PermissionOverwrites = require('./PermissionOverwrites');
|
||||
const EvaluatedPermissions = require('./EvaluatedPermissions');
|
||||
const Constants = require('../util/Constants');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
/**
|
||||
@@ -51,12 +50,12 @@ class GuildChannel extends Channel {
|
||||
* Gets the overall set of permissions for a user in this channel, taking into account roles and permission
|
||||
* overwrites.
|
||||
* @param {GuildMemberResolvable} member The user that you want to obtain the overall permissions for
|
||||
* @returns {?EvaluatedPermissions}
|
||||
* @returns {?Permissions}
|
||||
*/
|
||||
permissionsFor(member) {
|
||||
member = this.client.resolver.resolveGuildMember(this.guild, member);
|
||||
if (!member) return null;
|
||||
if (member.id === this.guild.ownerID) return new EvaluatedPermissions(member, Constants.ALL_PERMISSIONS);
|
||||
if (member.id === this.guild.ownerID) return new Permissions(member, Permissions.ALL);
|
||||
|
||||
let permissions = 0;
|
||||
|
||||
@@ -71,10 +70,10 @@ class GuildChannel extends Channel {
|
||||
}
|
||||
permissions |= allow;
|
||||
|
||||
const admin = Boolean(permissions & Constants.PermissionFlags.ADMINISTRATOR);
|
||||
if (admin) permissions = Constants.ALL_PERMISSIONS;
|
||||
const admin = Boolean(permissions & Permissions.FLAGS.ADMINISTRATOR);
|
||||
if (admin) permissions = Permissions.ALL;
|
||||
|
||||
return new EvaluatedPermissions(member, permissions);
|
||||
return new Permissions(member, permissions);
|
||||
}
|
||||
|
||||
overwritesFor(member, verified = false, roles = null) {
|
||||
@@ -151,14 +150,14 @@ class GuildChannel extends Channel {
|
||||
|
||||
for (const perm in options) {
|
||||
if (options[perm] === true) {
|
||||
payload.allow |= Constants.PermissionFlags[perm] || 0;
|
||||
payload.deny &= ~(Constants.PermissionFlags[perm] || 0);
|
||||
payload.allow |= Permissions.FLAGS[perm] || 0;
|
||||
payload.deny &= ~(Permissions.FLAGS[perm] || 0);
|
||||
} else if (options[perm] === false) {
|
||||
payload.allow &= ~(Constants.PermissionFlags[perm] || 0);
|
||||
payload.deny |= Constants.PermissionFlags[perm] || 0;
|
||||
payload.allow &= ~(Permissions.FLAGS[perm] || 0);
|
||||
payload.deny |= Permissions.FLAGS[perm] || 0;
|
||||
} else if (options[perm] === null) {
|
||||
payload.allow &= ~(Constants.PermissionFlags[perm] || 0);
|
||||
payload.deny &= ~(Constants.PermissionFlags[perm] || 0);
|
||||
payload.allow &= ~(Permissions.FLAGS[perm] || 0);
|
||||
payload.deny &= ~(Permissions.FLAGS[perm] || 0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,7 +293,7 @@ class GuildChannel extends Channel {
|
||||
*/
|
||||
get deletable() {
|
||||
return this.id !== this.guild.id &&
|
||||
this.permissionsFor(this.client.user).hasPermission(Constants.PermissionFlags.MANAGE_CHANNELS);
|
||||
this.permissionsFor(this.client.user).hasPermission(Permissions.FLAGS.MANAGE_CHANNELS);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||
const Role = require('./Role');
|
||||
const EvaluatedPermissions = require('./EvaluatedPermissions');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Constants = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
const Presence = require('./Presence').Presence;
|
||||
@@ -241,20 +241,17 @@ class GuildMember {
|
||||
|
||||
/**
|
||||
* The overall set of permissions for the guild member, taking only roles into account
|
||||
* @type {EvaluatedPermissions}
|
||||
* @type {Permissions}
|
||||
* @readonly
|
||||
*/
|
||||
get permissions() {
|
||||
if (this.user.id === this.guild.ownerID) return new EvaluatedPermissions(this, Constants.ALL_PERMISSIONS);
|
||||
if (this.user.id === this.guild.ownerID) return new Permissions(this, Permissions.ALL);
|
||||
|
||||
let permissions = 0;
|
||||
const roles = this.roles;
|
||||
for (const role of roles.values()) permissions |= role.permissions;
|
||||
|
||||
const admin = Boolean(permissions & Constants.PermissionFlags.ADMINISTRATOR);
|
||||
if (admin) permissions = Constants.ALL_PERMISSIONS;
|
||||
|
||||
return new EvaluatedPermissions(this, permissions);
|
||||
return new Permissions(this, permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -266,7 +263,7 @@ class GuildMember {
|
||||
if (this.user.id === this.guild.ownerID) return false;
|
||||
if (this.user.id === this.client.user.id) return false;
|
||||
const clientMember = this.guild.member(this.client.user);
|
||||
if (!clientMember.hasPermission(Constants.PermissionFlags.KICK_MEMBERS)) return false;
|
||||
if (!clientMember.hasPermission(Permissions.FLAGS.KICK_MEMBERS)) return false;
|
||||
return clientMember.highestRole.comparePositionTo(this.highestRole) > 0;
|
||||
}
|
||||
|
||||
@@ -279,14 +276,14 @@ class GuildMember {
|
||||
if (this.user.id === this.guild.ownerID) return false;
|
||||
if (this.user.id === this.client.user.id) return false;
|
||||
const clientMember = this.guild.member(this.client.user);
|
||||
if (!clientMember.hasPermission(Constants.PermissionFlags.BAN_MEMBERS)) return false;
|
||||
if (!clientMember.hasPermission(Permissions.FLAGS.BAN_MEMBERS)) return false;
|
||||
return clientMember.highestRole.comparePositionTo(this.highestRole) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `channel.permissionsFor(guildMember)`. Returns evaluated permissions for a member in a guild channel.
|
||||
* @param {ChannelResolvable} channel Guild channel to use as context
|
||||
* @returns {?EvaluatedPermissions}
|
||||
* @returns {?Permissions}
|
||||
*/
|
||||
permissionsIn(channel) {
|
||||
channel = this.client.resolver.resolveChannel(channel);
|
||||
|
||||
@@ -4,6 +4,7 @@ const MessageReaction = require('./MessageReaction');
|
||||
const Util = require('../util/Util');
|
||||
const Collection = require('../util/Collection');
|
||||
const Constants = require('../util/Constants');
|
||||
const Permissions = require('../util/Permissions');
|
||||
let GuildMember;
|
||||
|
||||
/**
|
||||
@@ -334,7 +335,7 @@ class Message {
|
||||
*/
|
||||
get deletable() {
|
||||
return this.author.id === this.client.user.id || (this.guild &&
|
||||
this.channel.permissionsFor(this.client.user).hasPermission(Constants.PermissionFlags.MANAGE_MESSAGES)
|
||||
this.channel.permissionsFor(this.client.user).hasPermission(Permissions.FLAGS.MANAGE_MESSAGES)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -345,7 +346,7 @@ class Message {
|
||||
*/
|
||||
get pinnable() {
|
||||
return !this.guild ||
|
||||
this.channel.permissionsFor(this.client.user).hasPermission(Constants.PermissionFlags.MANAGE_MESSAGES);
|
||||
this.channel.permissionsFor(this.client.user).hasPermission(Permissions.FLAGS.MANAGE_MESSAGES);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const Permissions = require('../util/Permissions');
|
||||
|
||||
/**
|
||||
* Represents a role on Discord
|
||||
@@ -118,7 +118,7 @@ class Role {
|
||||
get editable() {
|
||||
if (this.managed) return false;
|
||||
const clientMember = this.guild.member(this.client.user);
|
||||
if (!clientMember.hasPermission(Constants.PermissionFlags.MANAGE_ROLES_OR_PERMISSIONS)) return false;
|
||||
if (!clientMember.hasPermission(Permissions.FLAGS.MANAGE_ROLES_OR_PERMISSIONS)) return false;
|
||||
return clientMember.highestRole.comparePositionTo(this) > 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user