mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
Channel permissionLock support (syncing with category permissions) (#1924)
* add permissionsLocked * wip permissionLock() * should be good * better method name * see if this fixes channel jumping * fix property names * each overwrite is a different instance, and thus the pointers do not equal, even if the values do. * add more documentation to the edit method
This commit is contained in:
@@ -79,6 +79,7 @@ const Messages = {
|
|||||||
MESSAGE_SPLIT_MISSING: 'Message exceeds the max length and contains no split characters.',
|
MESSAGE_SPLIT_MISSING: 'Message exceeds the max length and contains no split characters.',
|
||||||
|
|
||||||
GUILD_CHANNEL_RESOLVE: 'Could not resolve channel to a guild channel.',
|
GUILD_CHANNEL_RESOLVE: 'Could not resolve channel to a guild channel.',
|
||||||
|
GUILD_CHANNEL_ORPHAN: 'Could not find a parent to this guild channel.',
|
||||||
GUILD_OWNED: 'Guild is owned by the client.',
|
GUILD_OWNED: 'Guild is owned by the client.',
|
||||||
GUILD_RESTRICTED: (state = false) => `Guild is ${state ? 'already' : 'not'} restricted.`,
|
GUILD_RESTRICTED: (state = false) => `Guild is ${state ? 'already' : 'not'} restricted.`,
|
||||||
GUILD_MEMBERS_TIMEOUT: 'Members didn\'t arrive in time.',
|
GUILD_MEMBERS_TIMEOUT: 'Members didn\'t arrive in time.',
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ const Util = require('../util/Util');
|
|||||||
const Permissions = require('../util/Permissions');
|
const Permissions = require('../util/Permissions');
|
||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const { TypeError } = require('../errors');
|
const { Error, TypeError } = require('../errors');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a guild channel (e.g. text channels and voice channels).
|
* Represents a guild channel (e.g. text channels and voice channels).
|
||||||
@@ -65,6 +65,22 @@ class GuildChannel extends Channel {
|
|||||||
return this.guild.channels.get(this.parentID);
|
return this.guild.channels.get(this.parentID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If the permissionOverwrites match the parent channel, null if no parent
|
||||||
|
* @type {?boolean}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
get permissionsLocked() {
|
||||||
|
if (!this.parent) return null;
|
||||||
|
if (this.permissionOverwrites.size !== this.parent.permissionOverwrites.size) return false;
|
||||||
|
return !this.permissionOverwrites.find((value, key) => {
|
||||||
|
const testVal = this.parent.permissionOverwrites.get(key);
|
||||||
|
return testVal === undefined ||
|
||||||
|
testVal.denied.bitfield !== value.denied.bitfield ||
|
||||||
|
testVal.allowed.bitfield !== value.allowed.bitfield;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The position of the channel
|
* The position of the channel
|
||||||
* @type {number}
|
* @type {number}
|
||||||
@@ -196,6 +212,21 @@ class GuildChannel extends Channel {
|
|||||||
.then(() => this);
|
.then(() => this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locks in the permission overwrites from the parent channel.
|
||||||
|
* @returns {Promise<GuildChannel>}
|
||||||
|
*/
|
||||||
|
lockPermissions() {
|
||||||
|
if (!this.parent) return Promise.reject(new Error('GUILD_CHANNEL_ORPHAN'));
|
||||||
|
const permissionOverwrites = this.parent.permissionOverwrites.map(overwrite => ({
|
||||||
|
deny: overwrite.denied.bitfield,
|
||||||
|
allow: overwrite.allowed.bitfield,
|
||||||
|
id: overwrite.id,
|
||||||
|
type: overwrite.type,
|
||||||
|
}));
|
||||||
|
return this.edit({ permissionOverwrites });
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A collection of members that can see this channel, mapped by their ID
|
* A collection of members that can see this channel, mapped by their ID
|
||||||
* @type {Collection<Snowflake, GuildMember>}
|
* @type {Collection<Snowflake, GuildMember>}
|
||||||
@@ -221,6 +252,16 @@ class GuildChannel extends Channel {
|
|||||||
* @property {number} [userLimit] The user limit of the voice channel
|
* @property {number} [userLimit] The user limit of the voice channel
|
||||||
* @property {Snowflake} [parentID] The parent ID of the channel
|
* @property {Snowflake} [parentID] The parent ID of the channel
|
||||||
* @property {boolean} [lockPermissions] Lock the permissions of the channel to what the parent's permissions are
|
* @property {boolean} [lockPermissions] Lock the permissions of the channel to what the parent's permissions are
|
||||||
|
* @property {OverwriteData[]} [permissionOverwrites] An array of overwrites to set for the channel
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The data for a permission overwrite
|
||||||
|
* @typedef {Object} OverwriteData
|
||||||
|
* @property {string} id The id of the overwrite
|
||||||
|
* @property {string} type The type of the overwrite, either role or member
|
||||||
|
* @property {number} allow The bitfield for the allowed permissions
|
||||||
|
* @property {number} deny The bitfield for the denied permissions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -239,11 +280,12 @@ class GuildChannel extends Channel {
|
|||||||
data: {
|
data: {
|
||||||
name: (data.name || this.name).trim(),
|
name: (data.name || this.name).trim(),
|
||||||
topic: data.topic,
|
topic: data.topic,
|
||||||
position: data.position || this.position,
|
position: data.position || this.rawPosition,
|
||||||
bitrate: data.bitrate || (this.bitrate ? this.bitrate * 1000 : undefined),
|
bitrate: data.bitrate || (this.bitrate ? this.bitrate * 1000 : undefined),
|
||||||
user_limit: data.userLimit != null ? data.userLimit : this.userLimit, // eslint-disable-line eqeqeq
|
user_limit: data.userLimit != null ? data.userLimit : this.userLimit, // eslint-disable-line eqeqeq
|
||||||
parent_id: data.parentID,
|
parent_id: data.parentID,
|
||||||
lock_permissions: data.lockPermissions,
|
lock_permissions: data.lockPermissions,
|
||||||
|
permission_overwrites: data.permissionOverwrites,
|
||||||
},
|
},
|
||||||
reason,
|
reason,
|
||||||
}).then(newData => {
|
}).then(newData => {
|
||||||
|
|||||||
Reference in New Issue
Block a user