From 91dd4319dd613f3fd1c200f66f35ef95a9083686 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Sat, 11 Nov 2023 21:30:49 +0000 Subject: [PATCH] fix: fix crashes when an array is supplied and remove assertion --- .../selectMenu/ChannelSelectMenu.ts | 30 +++++++++++-------- .../selectMenu/MentionableSelectMenu.ts | 28 ++++++++++------- .../components/selectMenu/RoleSelectMenu.ts | 26 ++++++++++------ .../components/selectMenu/UserSelectMenu.ts | 26 ++++++++++------ 4 files changed, 69 insertions(+), 41 deletions(-) diff --git a/packages/builders/src/components/selectMenu/ChannelSelectMenu.ts b/packages/builders/src/components/selectMenu/ChannelSelectMenu.ts index 56fb25d423e2..0a0a3ecc0aaf 100644 --- a/packages/builders/src/components/selectMenu/ChannelSelectMenu.ts +++ b/packages/builders/src/components/selectMenu/ChannelSelectMenu.ts @@ -1,11 +1,11 @@ -import type { - APIChannelSelectComponent, - APISelectMenuDefaultValue, - ChannelType, - Snowflake, +import { + type APIChannelSelectComponent, + type ChannelType, + type Snowflake, + ComponentType, + SelectMenuDefaultValueType, } from 'discord-api-types/v10'; -import { ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10'; -import { normalizeArray, type RestOrArray } from '../../util/normalizeArray.js'; +import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js'; import { channelTypesValidator, customIdValidator, optionsLengthValidator } from '../Assertions.js'; import { BaseSelectMenuBuilder } from './BaseSelectMenu.js'; @@ -70,9 +70,11 @@ export class ChannelSelectMenuBuilder extends BaseSelectMenuBuilder) { - const normalizedValues = channels.map((id) => { - return { id, type: SelectMenuDefaultValueType.Channel }; - }) as APISelectMenuDefaultValue[]; + const normalizedValues = normalizeArray(channels).map((id) => ({ + id, + type: SelectMenuDefaultValueType.Channel as const, + })); + this.data.default_values ??= []; optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length); this.data.default_values.push(...normalizedValues); @@ -85,9 +87,11 @@ export class ChannelSelectMenuBuilder extends BaseSelectMenuBuilder) { - const normalizedValues = channels.map((id) => { - return { id, type: SelectMenuDefaultValueType.Channel }; - }) as APISelectMenuDefaultValue[]; + const normalizedValues = normalizeArray(channels).map((id) => ({ + id, + type: SelectMenuDefaultValueType.Channel as const, + })); + optionsLengthValidator.parse(normalizedValues.length); this.data.default_values ??= []; this.data.default_values.splice(0, this.data.default_values.length, ...normalizedValues); diff --git a/packages/builders/src/components/selectMenu/MentionableSelectMenu.ts b/packages/builders/src/components/selectMenu/MentionableSelectMenu.ts index f90ad443bc5b..1f0b3736c7b8 100644 --- a/packages/builders/src/components/selectMenu/MentionableSelectMenu.ts +++ b/packages/builders/src/components/selectMenu/MentionableSelectMenu.ts @@ -1,7 +1,11 @@ -import type { APIMentionableSelectComponent, APISelectMenuDefaultValue, Snowflake } from 'discord-api-types/v10'; -import { ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10'; -import type { RestOrArray } from '../../index.js'; -import { normalizeArray } from '../../index.js'; +import { + type APIMentionableSelectComponent, + type APISelectMenuDefaultValue, + type Snowflake, + ComponentType, + SelectMenuDefaultValueType, +} from 'discord-api-types/v10'; +import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js'; import { optionsLengthValidator } from '../Assertions.js'; import { BaseSelectMenuBuilder } from './BaseSelectMenu.js'; @@ -41,9 +45,11 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder) { - const normalizedValues = roles.map((id) => { - return { id, type: SelectMenuDefaultValueType.Role }; - }) as APISelectMenuDefaultValue[]; + const normalizedValues = normalizeArray(roles).map((id) => ({ + id, + type: SelectMenuDefaultValueType.Role as const, + })); + this.data.default_values ??= []; optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length); this.data.default_values.push(...normalizedValues); @@ -56,9 +62,11 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder) { - const normalizedValues = users.map((id) => { - return { id, type: SelectMenuDefaultValueType.User }; - }) as APISelectMenuDefaultValue[]; + const normalizedValues = normalizeArray(users).map((id) => ({ + id, + type: SelectMenuDefaultValueType.User as const, + })); + this.data.default_values ??= []; optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length); this.data.default_values.push(...normalizedValues); diff --git a/packages/builders/src/components/selectMenu/RoleSelectMenu.ts b/packages/builders/src/components/selectMenu/RoleSelectMenu.ts index 658e76d05807..271bc09bab00 100644 --- a/packages/builders/src/components/selectMenu/RoleSelectMenu.ts +++ b/packages/builders/src/components/selectMenu/RoleSelectMenu.ts @@ -1,6 +1,10 @@ -import type { APIRoleSelectComponent, APISelectMenuDefaultValue, Snowflake } from 'discord-api-types/v10'; -import { ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10'; -import type { RestOrArray } from '../../index.js'; +import { + type APIRoleSelectComponent, + type Snowflake, + ComponentType, + SelectMenuDefaultValueType, +} from 'discord-api-types/v10'; +import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js'; import { optionsLengthValidator } from '../Assertions.js'; import { BaseSelectMenuBuilder } from './BaseSelectMenu.js'; @@ -40,9 +44,11 @@ export class RoleSelectMenuBuilder extends BaseSelectMenuBuilder) { - const normalizedValues = roles.map((id) => { - return { id, type: SelectMenuDefaultValueType.Role }; - }) as APISelectMenuDefaultValue[]; + const normalizedValues = normalizeArray(roles).map((id) => ({ + id, + type: SelectMenuDefaultValueType.Role as const, + })); + this.data.default_values ??= []; optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length); this.data.default_values.push(...normalizedValues); @@ -55,9 +61,11 @@ export class RoleSelectMenuBuilder extends BaseSelectMenuBuilder) { - const normalizedValues = roles.map((id) => { - return { id, type: SelectMenuDefaultValueType.Role }; - }) as APISelectMenuDefaultValue[]; + const normalizedValues = normalizeArray(roles).map((id) => ({ + id, + type: SelectMenuDefaultValueType.Role as const, + })); + optionsLengthValidator.parse(normalizedValues.length); this.data.default_values ??= []; this.data.default_values.splice(0, this.data.default_values.length, ...normalizedValues); diff --git a/packages/builders/src/components/selectMenu/UserSelectMenu.ts b/packages/builders/src/components/selectMenu/UserSelectMenu.ts index 8d12c909e847..4ae071b70ba7 100644 --- a/packages/builders/src/components/selectMenu/UserSelectMenu.ts +++ b/packages/builders/src/components/selectMenu/UserSelectMenu.ts @@ -1,6 +1,10 @@ -import type { APISelectMenuDefaultValue, APIUserSelectComponent, Snowflake } from 'discord-api-types/v10'; -import { ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10'; -import type { RestOrArray } from '../../index.js'; +import { + type APIUserSelectComponent, + type Snowflake, + ComponentType, + SelectMenuDefaultValueType, +} from 'discord-api-types/v10'; +import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js'; import { optionsLengthValidator } from '../Assertions.js'; import { BaseSelectMenuBuilder } from './BaseSelectMenu.js'; @@ -40,9 +44,11 @@ export class UserSelectMenuBuilder extends BaseSelectMenuBuilder) { - const normalizedValues = users.map((id) => { - return { id, type: SelectMenuDefaultValueType.User }; - }) as APISelectMenuDefaultValue[]; + const normalizedValues = normalizeArray(users).map((id) => ({ + id, + type: SelectMenuDefaultValueType.User as const, + })); + this.data.default_values ??= []; optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length); this.data.default_values.push(...normalizedValues); @@ -55,9 +61,11 @@ export class UserSelectMenuBuilder extends BaseSelectMenuBuilder) { - const normalizedValues = users.map((id) => { - return { id, type: SelectMenuDefaultValueType.User }; - }) as APISelectMenuDefaultValue[]; + const normalizedValues = normalizeArray(users).map((id) => ({ + id, + type: SelectMenuDefaultValueType.User as const, + })); + optionsLengthValidator.parse(normalizedValues.length); this.data.default_values ??= []; this.data.default_values.splice(0, this.data.default_values.length, ...normalizedValues);