Skip to content

chore(Client): robust error checking #9390

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/discord.js/src/client/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions packages/discord.js/src/errors/ErrorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
* @property {'ModalSubmitInteractionFieldType'} ModalSubmitInteractionFieldType

* @property {'InvalidMissingScopes'} InvalidMissingScopes
* @property {'InvalidScopesWithPermissions'} InvalidScopesWithPermissions

* @property {'NotImplemented'} NotImplemented

Expand Down Expand Up @@ -289,6 +290,7 @@ const keys = [
'ModalSubmitInteractionFieldType',

'InvalidMissingScopes',
'InvalidScopesWithPermissions',

'NotImplemented',

Expand Down
1 change: 1 addition & 0 deletions packages/discord.js/src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`,

Expand Down