From 7f3495cdefc0c384419211ff7ba54e14a2bf28ef Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Tue, 6 Apr 2021 22:19:36 +0530 Subject: [PATCH 01/10] feat(GuildChannel): make createOverwrite and updateOverwrite not dependent on cache --- src/structures/GuildChannel.js | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index fb0979cc3329..0011ec66a8bf 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -218,6 +218,7 @@ class GuildChannel extends Channel { * @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 {string} [type] The type of overwrite: `member` or `role` * @returns {Promise} * @example * // Update or Create permission overwrites for a message author @@ -227,15 +228,19 @@ 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')); + async updateOverwrite(userOrRole, options, reason, type) { + let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); + if (!type) { + 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; + } - const existing = this.permissionOverwrites.get(userOrRole.id); + 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, reason, type); } return this; } @@ -245,6 +250,7 @@ class GuildChannel extends Channel { * @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 {string} [type] The type of overwrite: `member` or `role` * @returns {Promise} * @example * // Create or Replace permission overwrites for a message author @@ -254,19 +260,22 @@ 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, reason, type) { + let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); + if (!type) { + 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, From 319c31284a061f3fc9874690b51456c7480d3955 Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Tue, 6 Apr 2021 23:33:27 +0530 Subject: [PATCH 02/10] refactor: remove redundant check for type parameter --- src/structures/GuildChannel.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 0011ec66a8bf..1fb232916bcb 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -229,12 +229,7 @@ class GuildChannel extends Channel { * .catch(console.error); */ async updateOverwrite(userOrRole, options, reason, type) { - let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); - if (!type) { - 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; - } + const userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); const existing = this.permissionOverwrites.get(userOrRoleID); if (existing) { From 0bacaa8d918ec98be031a3e20b9aed3dd6c4754d Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Wed, 7 Apr 2021 20:50:16 +0530 Subject: [PATCH 03/10] refactor: use a single object for optional parameters --- src/structures/GuildChannel.js | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 1fb232916bcb..703e8db9b530 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -213,12 +213,19 @@ class GuildChannel extends Channel { return this; } + /** + * Extra information about the overwrite + * @typedef {Object} ExtraOverwriteInfo + * @property {string} [reason] Reason for creating/editing this overwrite + * @property {string} [type] The type of overwrite, either `member` or `role`. 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 {string} [type] The type of overwrite: `member` or `role` + * @param {ExtraOverwriteInfo} [extraInfo] The extra information for the update * @returns {Promise} * @example * // Update or Create permission overwrites for a message author @@ -228,14 +235,14 @@ class GuildChannel extends Channel { * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .catch(console.error); */ - async updateOverwrite(userOrRole, options, reason, type) { + async updateOverwrite(userOrRole, options, extraInfo) { const userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); - + let { reason } = extraInfo; const existing = this.permissionOverwrites.get(userOrRoleID); if (existing) { await existing.update(options, reason); } else { - await this.createOverwrite(userOrRole, options, reason, type); + await this.createOverwrite(userOrRole, options, extraInfo); } return this; } @@ -244,8 +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 {string} [type] The type of overwrite: `member` or `role` + * @param {ExtraOverwriteInfo} [extraInfo] The extra information for the update * @returns {Promise} * @example * // Create or Replace permission overwrites for a message author @@ -255,8 +261,9 @@ class GuildChannel extends Channel { * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .catch(console.error); */ - createOverwrite(userOrRole, options, reason, type) { + createOverwrite(userOrRole, options, extraInfo) { let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); + let { type, reason } = extraInfo; if (!type) { 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')); From 15179ca77bb4f2f2cedffcb19208d9aee23430db Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Wed, 14 Apr 2021 16:35:17 +0530 Subject: [PATCH 04/10] refactor: use number instead of string for type field Co-authored-by: SpaceEEC --- src/structures/GuildChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 703e8db9b530..1727b22631d9 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -217,7 +217,7 @@ class GuildChannel extends Channel { * Extra information about the overwrite * @typedef {Object} ExtraOverwriteInfo * @property {string} [reason] Reason for creating/editing this overwrite - * @property {string} [type] The type of overwrite, either `member` or `role`. Use this to bypass automatic + * @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 */ From 1fad7e40007358500a0413d5a3666fd55d99bcdd Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Thu, 15 Apr 2021 12:35:07 +0530 Subject: [PATCH 05/10] docs: add suggested change in description to pass the ci MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/structures/GuildChannel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 1727b22631d9..cff4fd41326c 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -217,8 +217,8 @@ class GuildChannel extends Channel { * Extra information about the overwrite * @typedef {Object} ExtraOverwriteInfo * @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 + * @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 */ /** From 500f1e6ea2e3a84c7f9b4312acf131a5bde03e16 Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Thu, 15 Apr 2021 21:11:36 +0530 Subject: [PATCH 06/10] feat: add typings for ExtraOverwriteInfo and some minor fixes --- src/structures/GuildChannel.js | 6 +++--- typings/index.d.ts | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index cff4fd41326c..123595119db0 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -235,9 +235,9 @@ class GuildChannel extends Channel { * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .catch(console.error); */ - async updateOverwrite(userOrRole, options, extraInfo) { + async updateOverwrite(userOrRole, options, extraInfo = {}) { const userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); - let { reason } = extraInfo; + const { reason } = extraInfo; const existing = this.permissionOverwrites.get(userOrRoleID); if (existing) { await existing.update(options, reason); @@ -261,7 +261,7 @@ class GuildChannel extends Channel { * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .catch(console.error); */ - createOverwrite(userOrRole, options, extraInfo) { + createOverwrite(userOrRole, options, extraInfo = {}) { let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); let { type, reason } = extraInfo; if (!type) { diff --git a/typings/index.d.ts b/typings/index.d.ts index 1add8b580dc2..017f8d964a5a 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, + extraInfo?: ExtraOverwriteInfo, ): 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, + extraInfo?: ExtraOverwriteInfo, ): Promise; public isText(): this is TextChannel | NewsChannel; } @@ -2634,6 +2634,11 @@ declare module 'discord.js' { User: typeof User; } + interface ExtraOverwriteInfo { + reason?: string; + type?: number; + } + interface FetchMemberOptions { user: UserResolvable; cache?: boolean; From e9e2867d2722867ce0ab4c0c0725b3b68b0658ad Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Fri, 16 Apr 2021 02:47:14 +0530 Subject: [PATCH 07/10] fix: check value of type correctly --- src/structures/GuildChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 123595119db0..91a20a6a07e9 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -264,7 +264,7 @@ class GuildChannel extends Channel { createOverwrite(userOrRole, options, extraInfo = {}) { let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); let { type, reason } = extraInfo; - if (!type) { + if (!(type === 0 || type === 1)) { 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; From 51355e92af9334566f847d36637370b2912ec6d9 Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Fri, 16 Apr 2021 14:01:16 +0530 Subject: [PATCH 08/10] refactor: allow any number for the type field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/structures/GuildChannel.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 91a20a6a07e9..346df039efed 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -264,7 +264,7 @@ class GuildChannel extends Channel { createOverwrite(userOrRole, options, extraInfo = {}) { let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); let { type, reason } = extraInfo; - if (!(type === 0 || type === 1)) { + 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; From deeb9610736968efe1064e5951e27aa3acd5e76e Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Mon, 10 May 2021 20:17:35 +0530 Subject: [PATCH 09/10] refactor(ExtraOverwriteInfo): rename the typdef as suggested MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/structures/GuildChannel.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 346df039efed..9f5e53a3b112 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -215,7 +215,7 @@ class GuildChannel extends Channel { /** * Extra information about the overwrite - * @typedef {Object} ExtraOverwriteInfo + * @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 @@ -225,7 +225,7 @@ class GuildChannel extends Channel { * 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 {ExtraOverwriteInfo} [extraInfo] The extra information for the update + * @param {GuildChannelOverwriteOptions} [overwriteOptions] The extra information for the update * @returns {Promise} * @example * // Update or Create permission overwrites for a message author From 6f9be1c1e82c9e0b316d83894c40664f006c7fe8 Mon Sep 17 00:00:00 2001 From: Shubham Parihar Date: Mon, 10 May 2021 20:31:45 +0530 Subject: [PATCH 10/10] refactor: add the suggested change in more places --- src/structures/GuildChannel.js | 12 ++++++------ typings/index.d.ts | 6 +++--- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/structures/GuildChannel.js b/src/structures/GuildChannel.js index 9f5e53a3b112..c4dabaae8fab 100644 --- a/src/structures/GuildChannel.js +++ b/src/structures/GuildChannel.js @@ -235,14 +235,14 @@ class GuildChannel extends Channel { * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .catch(console.error); */ - async updateOverwrite(userOrRole, options, extraInfo = {}) { + async updateOverwrite(userOrRole, options, overwriteOptions = {}) { const userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); - const { reason } = extraInfo; + const { reason } = overwriteOptions; const existing = this.permissionOverwrites.get(userOrRoleID); if (existing) { await existing.update(options, reason); } else { - await this.createOverwrite(userOrRole, options, extraInfo); + await this.createOverwrite(userOrRole, options, overwriteOptions); } return this; } @@ -251,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 {ExtraOverwriteInfo} [extraInfo] The extra information for the update + * @param {GuildChannelOverwriteOptions} [overwriteOptions] The extra information for the update * @returns {Promise} * @example * // Create or Replace permission overwrites for a message author @@ -261,9 +261,9 @@ class GuildChannel extends Channel { * .then(channel => console.log(channel.permissionOverwrites.get(message.author.id))) * .catch(console.error); */ - createOverwrite(userOrRole, options, extraInfo = {}) { + createOverwrite(userOrRole, options, overwriteOptions = {}) { let userOrRoleID = this.guild.roles.resolveID(userOrRole) || this.client.users.resolveID(userOrRole); - let { type, reason } = extraInfo; + 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')); diff --git a/typings/index.d.ts b/typings/index.d.ts index 017f8d964a5a..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, - extraInfo?: ExtraOverwriteInfo, + 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, - extraInfo?: ExtraOverwriteInfo, + overwriteOptions?: GuildChannelOverwriteOptions, ): Promise; public isText(): this is TextChannel | NewsChannel; } @@ -2634,7 +2634,7 @@ declare module 'discord.js' { User: typeof User; } - interface ExtraOverwriteInfo { + interface GuildChannelOverwriteOptions { reason?: string; type?: number; }