mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
feat: allow multiple permission overwrites when editing channel (#2370)
* feat: allow multiple permission overwrites when editing channel * undo Permissions#resolve change
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
const Channel = require('./Channel');
|
||||
const Role = require('./Role');
|
||||
const Invite = require('./Invite');
|
||||
const resolvePermissions = require('./shared/resolvePermissions');
|
||||
const PermissionOverwrites = require('./PermissionOverwrites');
|
||||
const Util = require('../util/Util');
|
||||
const Permissions = require('../util/Permissions');
|
||||
@@ -176,6 +177,28 @@ class GuildChannel extends Channel {
|
||||
.freeze();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the permission overwrites for a channel
|
||||
* @param {Object} [options] Options
|
||||
* @param {Array<PermissionOverwrites|PermissionOverwriteOptions>} [options.overwrites] Permission overwrites
|
||||
* @param {string} [options.reason] Reason for updating the channel overwrites
|
||||
* @returns {Promise<GuildChannel>}
|
||||
* @example
|
||||
* channel.overwritePermissions({
|
||||
* overwrites: [
|
||||
* {
|
||||
* id: message.author.id,
|
||||
* denied: ['VIEW_CHANNEL'],
|
||||
* },
|
||||
* ],
|
||||
* reason: 'Needed to change permissions'
|
||||
* });
|
||||
*/
|
||||
overwritePermissions({ overwrites, reason } = {}) {
|
||||
return this.edit({ permissionOverwrites: resolvePermissions.call(this, overwrites), reason })
|
||||
.then(() => this);
|
||||
}
|
||||
|
||||
/**
|
||||
* An object mapping permission flags to `true` (enabled), `null` (default) or `false` (disabled).
|
||||
* ```js
|
||||
@@ -185,24 +208,24 @@ class GuildChannel extends Channel {
|
||||
* 'ATTACH_FILES': false,
|
||||
* }
|
||||
* ```
|
||||
* @typedef {Object} PermissionOverwriteOptions
|
||||
* @typedef {Object} PermissionOverwriteOption
|
||||
*/
|
||||
|
||||
/**
|
||||
* Overwrites the permissions for a user or role in this channel.
|
||||
* @param {RoleResolvable|UserResolvable} userOrRole The user or role to update
|
||||
* @param {PermissionOverwriteOptions} options The options for the update
|
||||
* @param {PermissionOverwriteOption} options The options for the update
|
||||
* @param {string} [reason] Reason for creating/editing this overwrite
|
||||
* @returns {Promise<GuildChannel>}
|
||||
* @example
|
||||
* // Overwrite permissions for a message author
|
||||
* message.channel.overwritePermissions(message.author, {
|
||||
* message.channel.updateOverwrite(message.author, {
|
||||
* SEND_MESSAGES: false
|
||||
* })
|
||||
* .then(() => console.log('Done!'))
|
||||
* .then(channel => console.log(channel.permissionOverwrites.get(message.author.id)))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
overwritePermissions(userOrRole, options, reason) {
|
||||
updateOverwrite(userOrRole, options, reason) {
|
||||
const allow = new Permissions(0);
|
||||
const deny = new Permissions(0);
|
||||
let type;
|
||||
|
||||
26
src/structures/shared/resolvePermissions.js
Normal file
26
src/structures/shared/resolvePermissions.js
Normal file
@@ -0,0 +1,26 @@
|
||||
const Permissions = require('../../util/Permissions');
|
||||
const Collection = require('../../util/Collection');
|
||||
|
||||
module.exports = function resolvePermissions(overwrites) {
|
||||
if (overwrites instanceof Collection || overwrites instanceof Array) {
|
||||
overwrites = overwrites.map(overwrite => {
|
||||
const role = this.guild.roles.resolve(overwrite.id);
|
||||
if (role) {
|
||||
overwrite.id = role.id;
|
||||
overwrite.type = 'role';
|
||||
} else {
|
||||
overwrite.id = this.client.users.resolveID(overwrite.id);
|
||||
overwrite.type = 'member';
|
||||
}
|
||||
|
||||
return {
|
||||
allow: Permissions.resolve(overwrite.allowed || 0),
|
||||
deny: Permissions.resolve(overwrite.denied || 0),
|
||||
type: overwrite.type,
|
||||
id: overwrite.id,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return overwrites;
|
||||
};
|
||||
Reference in New Issue
Block a user