diff --git a/src/client/ClientDataResolver.js b/src/client/ClientDataResolver.js index c725792b7b7f..4f9978c8b7a6 100644 --- a/src/client/ClientDataResolver.js +++ b/src/client/ClientDataResolver.js @@ -11,6 +11,7 @@ const Channel = require('../structures/Channel'); const GuildMember = require('../structures/GuildMember'); const Emoji = require('../structures/Emoji'); const ReactionEmoji = require('../structures/ReactionEmoji'); +const Role = require('../structures/Role'); /** * The DataResolver identifies different objects and tries to resolve a specific piece of information from them, e.g. @@ -101,6 +102,27 @@ class ClientDataResolver { return guild.members.get(user.id) || null; } + /** + * Data that can be resolved to a Role object. This can be: + * * A Role + * * A Snowflake + * @typedef {Role|Snowflake} RoleResolvable + */ + + /** + * Resolves a RoleResolvable to a Role object. + * @param {GuildResolvable} guild The guild that this role is part of + * @param {RoleResolvable} role The role resolvable to resolve + * @returns {?Role} + */ + resolveRole(guild, role) { + if (role instanceof Role) return role; + guild = this.resolveGuild(guild); + if (!guild) return null; + if (typeof role === 'string') return guild.roles.get(role); + return null; + } + /** * Data that can be resolved to give a Channel object. This can be: * * A Channel object diff --git a/src/client/rest/RESTMethods.js b/src/client/rest/RESTMethods.js index 9b926a497919..4904f0eb3b60 100644 --- a/src/client/rest/RESTMethods.js +++ b/src/client/rest/RESTMethods.js @@ -253,7 +253,30 @@ class RESTMethods { } createChannel(guild, channelName, channelType, overwrites, reason) { - if (overwrites instanceof Collection) overwrites = overwrites.array(); + if (overwrites instanceof Collection || overwrites instanceof Array) { + overwrites = overwrites.map(overwrite => { + let allow = overwrite.allow || overwrite._allowed; + let deny = overwrite.deny || overwrite._denied; + if (allow instanceof Array) allow = Permissions.resolve(allow); + if (deny instanceof Array) deny = Permissions.resolve(deny); + + const role = this.client.resolver.resolveRole(this, overwrite.id); + if (role) { + overwrite.id = role.id; + overwrite.type = 'role'; + } else { + overwrite.id = this.client.resolver.resolveUserID(overwrite.id); + overwrite.type = 'member'; + } + + return { + allow, + deny, + type: overwrite.type, + id: overwrite.id, + }; + }); + } return this.rest.makeRequest('post', Endpoints.Guild(guild).channels, true, { name: channelName, type: Constants.ChannelTypes[channelType.toUpperCase()], diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 3b6844e6d4fb..374680739b46 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -873,11 +873,19 @@ class Guild { if (!this.client.user.bot) this.client.syncGuilds([this]); } + /** + * Can be used to overwrite permissions when creating a channel. + * @typedef {Object} ChannelCreationOverwrites + * @property {PermissionResolvable[]|number} [allow] The permissions to allow + * @property {PermissionResolvable[]|number} [deny] The permissions to deny + * @property {RoleResolvable|UserResolvable} id ID of the role or member this overwrite is for + */ + /** * Creates a new channel in the guild. * @param {string} name The name of the new channel * @param {string} type The type of the new channel, either `text` or `voice` or `category` - * @param {Array} [overwrites] Permission overwrites to apply to the new channel + * @param {Array} [overwrites] Permission overwrites * @param {string} [reason] Reason for creating this channel * @returns {Promise} * @example diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index b196dc423a31..48a5daa63a2e 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -155,7 +155,7 @@ class GuildChannel extends Channel { * @param {Role|Snowflake|UserResolvable} userOrRole The user or role to update * @param {PermissionOverwriteOptions} options The configuration for the update * @param {string} [reason] Reason for creating/editing this overwrite - * @returns {Promise} + * @returns {Promise} * @example * // Overwrite permissions for a message author * message.channel.overwritePermissions(message.author, { @@ -203,7 +203,7 @@ class GuildChannel extends Channel { } } - return this.client.rest.methods.setChannelOverwrite(this, payload, reason); + return this.client.rest.methods.setChannelOverwrite(this, payload, reason).then(() => this); } /**