chore(Client): robust error checking (#9390)

* chore: robust error checking

* fix: check for null

* chore: add period

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>

* fix: check for undefined correctly

* chore: robust error checking

* fix: check for null

* chore: add period

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>

* fix: check for undefined correctly

* fix: check for null

* fix: update typings

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
Tetie
2023-04-17 17:05:32 +02:00
committed by GitHub
parent 7b10e8cb63
commit 7d7972d239
4 changed files with 25 additions and 0 deletions

View File

@@ -420,6 +420,9 @@ class Client extends BaseClient {
if (!scopes.some(scope => [OAuth2Scopes.Bot, OAuth2Scopes.ApplicationsCommands].includes(scope))) { if (!scopes.some(scope => [OAuth2Scopes.Bot, OAuth2Scopes.ApplicationsCommands].includes(scope))) {
throw new DiscordjsTypeError(ErrorCodes.InvalidMissingScopes); 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 validScopes = Object.values(OAuth2Scopes);
const invalidScope = scopes.find(scope => !validScopes.includes(scope)); const invalidScope = scopes.find(scope => !validScopes.includes(scope));
if (invalidScope) { if (invalidScope) {
@@ -512,6 +515,24 @@ class Client extends BaseClient {
if (typeof options.failIfNotExists !== 'boolean') { if (typeof options.failIfNotExists !== 'boolean') {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean'); throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'failIfNotExists', 'a boolean');
} }
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) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'presence', 'an object');
}
if (typeof options.ws !== 'object' || options.ws === null) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'ws', 'an object');
}
if (typeof options.rest !== 'object' || options.rest === null) {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'rest', 'an object');
}
if (typeof options.jsonTransformer !== 'function') {
throw new DiscordjsTypeError(ErrorCodes.ClientInvalidOption, 'jsonTransformer', 'a function');
}
} }
} }

View File

@@ -142,6 +142,7 @@
* @property {'ModalSubmitInteractionFieldType'} ModalSubmitInteractionFieldType * @property {'ModalSubmitInteractionFieldType'} ModalSubmitInteractionFieldType
* @property {'InvalidMissingScopes'} InvalidMissingScopes * @property {'InvalidMissingScopes'} InvalidMissingScopes
* @property {'InvalidScopesWithPermissions'} InvalidScopesWithPermissions
* @property {'NotImplemented'} NotImplemented * @property {'NotImplemented'} NotImplemented
@@ -289,6 +290,7 @@ const keys = [
'ModalSubmitInteractionFieldType', 'ModalSubmitInteractionFieldType',
'InvalidMissingScopes', 'InvalidMissingScopes',
'InvalidScopesWithPermissions',
'NotImplemented', 'NotImplemented',

View File

@@ -155,6 +155,7 @@ const Messages = {
`Field with custom id "${customId}" is of type: ${type}; expected ${expected}.`, `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.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}.`, [DjsErrorCodes.NotImplemented]: (what, name) => `Method ${what} not implemented on ${name}.`,

View File

@@ -3636,6 +3636,7 @@ export enum DiscordjsErrorCodes {
ModalSubmitInteractionFieldType = 'ModalSubmitInteractionFieldType', ModalSubmitInteractionFieldType = 'ModalSubmitInteractionFieldType',
InvalidMissingScopes = 'InvalidMissingScopes', InvalidMissingScopes = 'InvalidMissingScopes',
InvalidScopesWithPermissions = 'InvalidScopesWithPermissions',
NotImplemented = 'NotImplemented', NotImplemented = 'NotImplemented',