diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index fb0979cc3329..c4dabaae8fab 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -213,11 +213,19 @@ class GuildChannel extends Channel { return this; } + /** + * Extra information about the overwrite + * @typedef {Object} GuildChannelOverwriteOptions + * @property {string} [reason] Reason for creating/editing this overwrite + * @property {number} [type] The type of overwrite, either `0` for a role or `1` for a member. Use this to bypass + * automatic resolution of type that results in an error for uncached structure + */ + /** * Updates permission overwrites for a user or role in this channel, or creates an entry if not already present. * @param {RoleResolvable|UserResolvable} userOrRole The user or role to update * @param {PermissionOverwriteOptions} options The options for the update - * @param {string} [reason] Reason for creating/editing this overwrite + * @param {GuildChannelOverwriteOptions} [overwriteOptions] The extra information for the update * @returns {Promise} * @example * // Update or Create permission overwrites for a message author @@ -227,15 +235,14 @@ class GuildChannel extends Channel { * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .catch(console.error); */ - async updateOverwrite(userOrRole, options, reason) { - userOrRole = this.guild.roles.resolve(userOrRole) || this.client.users.resolve(userOrRole); - if (!userOrRole) return Promise.reject(new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role')); - - const existing = this.permissionOverwrites.get(userOrRole.id); + async updateOverwrite(userOrRole, options, overwriteOptions = {}) { + const userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); + const { reason } = overwriteOptions; + const existing = this.permissionOverwrites.get(userOrRoleID); if (existing) { await existing.update(options, reason); } else { - await this.createOverwrite(userOrRole, options, reason); + await this.createOverwrite(userOrRole, options, overwriteOptions); } return this; } @@ -244,7 +251,7 @@ class GuildChannel extends Channel { * Creates permission overwrites for a user or role in this channel, or replaces them if already present. * @param {RoleResolvable|UserResolvable} userOrRole The user or role to update * @param {PermissionOverwriteOptions} options The options for the update - * @param {string} [reason] Reason for creating/editing this overwrite + * @param {GuildChannelOverwriteOptions} [overwriteOptions] The extra information for the update * @returns {Promise} * @example * // Create or Replace permission overwrites for a message author @@ -254,19 +261,23 @@ class GuildChannel extends Channel { * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .catch(console.error); */ - createOverwrite(userOrRole, options, reason) { - userOrRole = this.guild.roles.resolve(userOrRole) || this.client.users.resolve(userOrRole); - if (!userOrRole) return Promise.reject(new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role')); - - const type = userOrRole instanceof Role ? OverwriteTypes.role : OverwriteTypes.member; + createOverwrite(userOrRole, options, overwriteOptions = {}) { + let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); + let { type, reason } = overwriteOptions; + if (typeof type !== 'number') { + userOrRole = this.guild.roles.resolve(userOrRole) || this.client.users.resolve(userOrRole); + if (!userOrRole) return Promise.reject(new TypeError('INVALID_TYPE', 'parameter', 'User nor a Role')); + userOrRoleID = userOrRole.id; + type = userOrRole instanceof Role ? OverwriteTypes.role : OverwriteTypes.member; + } const { allow, deny } = PermissionOverwrites.resolveOverwriteOptions(options); return this.client.api .channels(this.id) - .permissions(userOrRole.id) + .permissions(userOrRoleID) .put({ data: { - id: userOrRole.id, + id: userOrRoleID, type, allow, deny, diff --git a/typings/index.d.ts b/typings/index.d.ts index 1add8b580dc2..674b6173d716 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -766,7 +766,7 @@ declare module 'discord.js' { public createOverwrite( userOrRole: RoleResolvable | UserResolvable, options: PermissionOverwriteOption, - reason?: string, + overwriteOptions?: GuildChannelOverwriteOptions, ): Promise; public edit(data: ChannelData, reason?: string): Promise; public equals(channel: GuildChannel): boolean; @@ -788,7 +788,7 @@ declare module 'discord.js' { public updateOverwrite( userOrRole: RoleResolvable | UserResolvable, options: PermissionOverwriteOption, - reason?: string, + overwriteOptions?: GuildChannelOverwriteOptions, ): Promise; public isText(): this is TextChannel | NewsChannel; } @@ -2634,6 +2634,11 @@ declare module 'discord.js' { User: typeof User; } + interface GuildChannelOverwriteOptions { + reason?: string; + type?: number; + } + interface FetchMemberOptions { user: UserResolvable; cache?: boolean;