From ecb068b2a8390996c70ca6d02c9e5c52f61d3055 Mon Sep 17 00:00:00 2001 From: TetieWasTaken Date: Fri, 14 Apr 2023 21:52:02 +0200 Subject: [PATCH 01/10] chore: robust error checking --- packages/discord.js/src/client/Client.js | 18 ++++++++++++++++++ packages/discord.js/src/errors/ErrorCodes.js | 2 ++ packages/discord.js/src/errors/Messages.js | 1 + 3 files changed, 21 insertions(+) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index 28f01a1a0dfb..b61b2244356e 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -420,6 +420,9 @@ class Client extends BaseClient { if (!scopes.some(scope => [OAuth2Scopes.Bot, OAuth2Scopes.ApplicationsCommands].includes(scope))) { throw new DiscordjsTypeError(ErrorCodes.InvalidMissingScopes); } + if (scopes.some(scope => ![OAuth2Scopes.Bot].includes(scope)) && options.permissions) { + throw new DiscordjsTypeError(ErrorCodes.InvalidScopeWithPermissions); + } const validScopes = Object.values(OAuth2Scopes); const invalidScope = scopes.find(scope => !validScopes.includes(scope)); if (invalidScope) { @@ -512,6 +515,21 @@ class Client extends BaseClient { if (typeof options.failIfNotExists !== 'boolean') { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean'); } + if (options.allowedMentions && typeof options.allowedMentions !== 'object') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'allowedMentions', 'an object'); + } + if (typeof options.presence !== 'object') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'presence', 'an object'); + } + if (typeof options.ws !== 'object') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws', 'an object'); + } + if (typeof options.rest !== 'object') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'rest', 'an object'); + } + if (typeof options.jsonTransformer !== 'function') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'jsonTransformer', 'a function'); + } } } diff --git a/packages/discord.js/src/errors/ErrorCodes.js b/packages/discord.js/src/errors/ErrorCodes.js index 3f074a1dad06..cb1a1f09d46e 100644 --- a/packages/discord.js/src/errors/ErrorCodes.js +++ b/packages/discord.js/src/errors/ErrorCodes.js @@ -142,6 +142,7 @@ * @property {'ModalSubmitInteractionFieldType'} ModalSubmitInteractionFieldType * @property {'InvalidMissingScopes'} InvalidMissingScopes + * @property {'InvalidScopesWithPermissions'} InvalidScopesWithPermissions * @property {'NotImplemented'} NotImplemented @@ -289,6 +290,7 @@ const keys = [ 'ModalSubmitInteractionFieldType', 'InvalidMissingScopes', + 'InvalidScopesWithPermissions', 'NotImplemented', diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 1b79ec030023..37535a371256 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -155,6 +155,7 @@ const Messages = { `Field with custom id "${customId}" is of type: ${type}; expected ${expected}.`, [DjsErrorCodes.InvalidMissingScopes]: 'At least one valid scope must be provided for the invite', + [DjsErrorCodes.InvalidScopesWithPermissions]: 'Permissions cannot be set without the bot scope', [DjsErrorCodes.NotImplemented]: (what, name) => `Method ${what} not implemented on ${name}.`, From bb4e73883b634f3c7462e6d724c55fb878937fe8 Mon Sep 17 00:00:00 2001 From: TetieWasTaken Date: Sat, 15 Apr 2023 09:37:15 +0200 Subject: [PATCH 02/10] fix: check for null --- packages/discord.js/src/client/Client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index b61b2244356e..8fd42a4cc1b1 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -518,13 +518,13 @@ class Client extends BaseClient { if (options.allowedMentions && typeof options.allowedMentions !== 'object') { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'allowedMentions', 'an object'); } - if (typeof options.presence !== 'object') { + if (typeof options.presence !== 'object' || options.presence === null) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'presence', 'an object'); } - if (typeof options.ws !== 'object') { + if (typeof options.ws !== 'object' || options.ws === null) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws', 'an object'); } - if (typeof options.rest !== 'object') { + if (typeof options.rest !== 'object' || options.rest === null) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'rest', 'an object'); } if (typeof options.jsonTransformer !== 'function') { From 1df8d902d9ac68ab3b7cb57494e25fc9b3275c03 Mon Sep 17 00:00:00 2001 From: Tetie Date: Sat, 15 Apr 2023 13:52:12 +0200 Subject: [PATCH 03/10] chore: add period Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> --- packages/discord.js/src/errors/Messages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 37535a371256..afdeafea8050 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -155,7 +155,7 @@ const Messages = { `Field with custom id "${customId}" is of type: ${type}; expected ${expected}.`, [DjsErrorCodes.InvalidMissingScopes]: 'At least one valid scope must be provided for the invite', - [DjsErrorCodes.InvalidScopesWithPermissions]: 'Permissions cannot be set without the bot scope', + [DjsErrorCodes.InvalidScopesWithPermissions]: 'Permissions cannot be set without the bot scope.', [DjsErrorCodes.NotImplemented]: (what, name) => `Method ${what} not implemented on ${name}.`, From c3dbd35bcffe4cf3c18d9d18941ff00032d0fb3c Mon Sep 17 00:00:00 2001 From: TetieWasTaken Date: Sat, 15 Apr 2023 14:30:09 +0200 Subject: [PATCH 04/10] fix: check for undefined correctly --- packages/discord.js/src/client/Client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index 8fd42a4cc1b1..587f71d830bd 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -515,7 +515,7 @@ class Client extends BaseClient { if (typeof options.failIfNotExists !== 'boolean') { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean'); } - if (options.allowedMentions && typeof options.allowedMentions !== 'object') { + if (typeof options.allowedMentions !== 'object' && options.allowedMentions !== undefined) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'allowedMentions', 'an object'); } if (typeof options.presence !== 'object' || options.presence === null) { From e7e067005aa3ea79deeca9271b2e2b1619d4a9c6 Mon Sep 17 00:00:00 2001 From: TetieWasTaken Date: Fri, 14 Apr 2023 21:52:02 +0200 Subject: [PATCH 05/10] chore: robust error checking --- packages/discord.js/src/client/Client.js | 18 ++++++++++++++++++ packages/discord.js/src/errors/ErrorCodes.js | 2 ++ packages/discord.js/src/errors/Messages.js | 1 + 3 files changed, 21 insertions(+) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index 28f01a1a0dfb..b61b2244356e 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -420,6 +420,9 @@ class Client extends BaseClient { if (!scopes.some(scope => [OAuth2Scopes.Bot, OAuth2Scopes.ApplicationsCommands].includes(scope))) { throw new DiscordjsTypeError(ErrorCodes.InvalidMissingScopes); } + if (scopes.some(scope => ![OAuth2Scopes.Bot].includes(scope)) && options.permissions) { + throw new DiscordjsTypeError(ErrorCodes.InvalidScopeWithPermissions); + } const validScopes = Object.values(OAuth2Scopes); const invalidScope = scopes.find(scope => !validScopes.includes(scope)); if (invalidScope) { @@ -512,6 +515,21 @@ class Client extends BaseClient { if (typeof options.failIfNotExists !== 'boolean') { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean'); } + if (options.allowedMentions && typeof options.allowedMentions !== 'object') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'allowedMentions', 'an object'); + } + if (typeof options.presence !== 'object') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'presence', 'an object'); + } + if (typeof options.ws !== 'object') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws', 'an object'); + } + if (typeof options.rest !== 'object') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'rest', 'an object'); + } + if (typeof options.jsonTransformer !== 'function') { + throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'jsonTransformer', 'a function'); + } } } diff --git a/packages/discord.js/src/errors/ErrorCodes.js b/packages/discord.js/src/errors/ErrorCodes.js index 3f074a1dad06..cb1a1f09d46e 100644 --- a/packages/discord.js/src/errors/ErrorCodes.js +++ b/packages/discord.js/src/errors/ErrorCodes.js @@ -142,6 +142,7 @@ * @property {'ModalSubmitInteractionFieldType'} ModalSubmitInteractionFieldType * @property {'InvalidMissingScopes'} InvalidMissingScopes + * @property {'InvalidScopesWithPermissions'} InvalidScopesWithPermissions * @property {'NotImplemented'} NotImplemented @@ -289,6 +290,7 @@ const keys = [ 'ModalSubmitInteractionFieldType', 'InvalidMissingScopes', + 'InvalidScopesWithPermissions', 'NotImplemented', diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 1b79ec030023..37535a371256 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -155,6 +155,7 @@ const Messages = { `Field with custom id "${customId}" is of type: ${type}; expected ${expected}.`, [DjsErrorCodes.InvalidMissingScopes]: 'At least one valid scope must be provided for the invite', + [DjsErrorCodes.InvalidScopesWithPermissions]: 'Permissions cannot be set without the bot scope', [DjsErrorCodes.NotImplemented]: (what, name) => `Method ${what} not implemented on ${name}.`, From 511a327ee9b1064c8849e10307d1256e7cfc95e4 Mon Sep 17 00:00:00 2001 From: TetieWasTaken Date: Sat, 15 Apr 2023 09:37:15 +0200 Subject: [PATCH 06/10] fix: check for null --- packages/discord.js/src/client/Client.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index b61b2244356e..8fd42a4cc1b1 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -518,13 +518,13 @@ class Client extends BaseClient { if (options.allowedMentions && typeof options.allowedMentions !== 'object') { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'allowedMentions', 'an object'); } - if (typeof options.presence !== 'object') { + if (typeof options.presence !== 'object' || options.presence === null) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'presence', 'an object'); } - if (typeof options.ws !== 'object') { + if (typeof options.ws !== 'object' || options.ws === null) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws', 'an object'); } - if (typeof options.rest !== 'object') { + if (typeof options.rest !== 'object' || options.rest === null) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'rest', 'an object'); } if (typeof options.jsonTransformer !== 'function') { From 4fe3141446b5f6ea4c82cac17a32bdba50ae7cce Mon Sep 17 00:00:00 2001 From: Tetie Date: Sat, 15 Apr 2023 13:52:12 +0200 Subject: [PATCH 07/10] chore: add period Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> --- packages/discord.js/src/errors/Messages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/errors/Messages.js b/packages/discord.js/src/errors/Messages.js index 37535a371256..afdeafea8050 100644 --- a/packages/discord.js/src/errors/Messages.js +++ b/packages/discord.js/src/errors/Messages.js @@ -155,7 +155,7 @@ const Messages = { `Field with custom id "${customId}" is of type: ${type}; expected ${expected}.`, [DjsErrorCodes.InvalidMissingScopes]: 'At least one valid scope must be provided for the invite', - [DjsErrorCodes.InvalidScopesWithPermissions]: 'Permissions cannot be set without the bot scope', + [DjsErrorCodes.InvalidScopesWithPermissions]: 'Permissions cannot be set without the bot scope.', [DjsErrorCodes.NotImplemented]: (what, name) => `Method ${what} not implemented on ${name}.`, From 26f404bc6e485767ff2774730ddb8d75a55b4d0e Mon Sep 17 00:00:00 2001 From: TetieWasTaken Date: Sat, 15 Apr 2023 14:30:09 +0200 Subject: [PATCH 08/10] fix: check for undefined correctly --- packages/discord.js/src/client/Client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index 8fd42a4cc1b1..587f71d830bd 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -515,7 +515,7 @@ class Client extends BaseClient { if (typeof options.failIfNotExists !== 'boolean') { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean'); } - if (options.allowedMentions && typeof options.allowedMentions !== 'object') { + if (typeof options.allowedMentions !== 'object' && options.allowedMentions !== undefined) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'allowedMentions', 'an object'); } if (typeof options.presence !== 'object' || options.presence === null) { From 85130a4a75454b19b2c3759eaa0acfb66e92e22e Mon Sep 17 00:00:00 2001 From: TetieWasTaken Date: Mon, 17 Apr 2023 14:44:54 +0200 Subject: [PATCH 09/10] fix: check for null --- packages/discord.js/src/client/Client.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/discord.js/src/client/Client.js b/packages/discord.js/src/client/Client.js index 587f71d830bd..ddf8d3b7e468 100644 --- a/packages/discord.js/src/client/Client.js +++ b/packages/discord.js/src/client/Client.js @@ -515,7 +515,10 @@ class Client extends BaseClient { if (typeof options.failIfNotExists !== 'boolean') { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean'); } - if (typeof options.allowedMentions !== 'object' && options.allowedMentions !== undefined) { + if ( + (typeof options.allowedMentions !== 'object' && options.allowedMentions !== undefined) || + options.allowedMentions === null + ) { throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'allowedMentions', 'an object'); } if (typeof options.presence !== 'object' || options.presence === null) { From 8902f6a8a5dd25cb50d37d69caab6a95ffb384a5 Mon Sep 17 00:00:00 2001 From: TetieWasTaken Date: Mon, 17 Apr 2023 16:58:20 +0200 Subject: [PATCH 10/10] fix: update typings --- packages/discord.js/typings/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 47f500f7e5c1..03dfb211761f 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -3636,6 +3636,7 @@ export enum DiscordjsErrorCodes { ModalSubmitInteractionFieldType = 'ModalSubmitInteractionFieldType', InvalidMissingScopes = 'InvalidMissingScopes', + InvalidScopesWithPermissions = 'InvalidScopesWithPermissions', NotImplemented = 'NotImplemented',