Skip to content

Commit

Permalink
fix: fix crashes when an array is supplied and remove assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiralite committed Nov 11, 2023
1 parent 045110b commit 91dd431
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 41 deletions.
30 changes: 17 additions & 13 deletions packages/builders/src/components/selectMenu/ChannelSelectMenu.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -70,9 +70,11 @@ export class ChannelSelectMenuBuilder extends BaseSelectMenuBuilder<APIChannelSe
* @param channels - The channels to add
*/
public addDefaultChannels(...channels: RestOrArray<Snowflake>) {
const normalizedValues = channels.map((id) => {
return { id, type: SelectMenuDefaultValueType.Channel };
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Channel>[];
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);
Expand All @@ -85,9 +87,11 @@ export class ChannelSelectMenuBuilder extends BaseSelectMenuBuilder<APIChannelSe
* @param channels - The channels to set
*/
public setDefaultChannels(...channels: RestOrArray<Snowflake>) {
const normalizedValues = channels.map((id) => {
return { id, type: SelectMenuDefaultValueType.Channel };
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Channel>[];
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -41,9 +45,11 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder<APIMenti
* @param roles - The roles to add
*/
public addDefaultRoles(...roles: RestOrArray<Snowflake>) {
const normalizedValues = roles.map((id) => {
return { id, type: SelectMenuDefaultValueType.Role };
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Role>[];
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);
Expand All @@ -56,9 +62,11 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder<APIMenti
* @param users - The users to add
*/
public addDefaultUsers(...users: RestOrArray<Snowflake>) {
const normalizedValues = users.map((id) => {
return { id, type: SelectMenuDefaultValueType.User };
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.User>[];
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);
Expand Down
26 changes: 17 additions & 9 deletions packages/builders/src/components/selectMenu/RoleSelectMenu.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -40,9 +44,11 @@ export class RoleSelectMenuBuilder extends BaseSelectMenuBuilder<APIRoleSelectCo
* @param roles - The roles to add
*/
public addDefaultRoles(...roles: RestOrArray<Snowflake>) {
const normalizedValues = roles.map((id) => {
return { id, type: SelectMenuDefaultValueType.Role };
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Role>[];
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);
Expand All @@ -55,9 +61,11 @@ export class RoleSelectMenuBuilder extends BaseSelectMenuBuilder<APIRoleSelectCo
* @param roles - The roles to set
*/
public setDefaultRoles(...roles: RestOrArray<Snowflake>) {
const normalizedValues = roles.map((id) => {
return { id, type: SelectMenuDefaultValueType.Role };
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Role>[];
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);
Expand Down
26 changes: 17 additions & 9 deletions packages/builders/src/components/selectMenu/UserSelectMenu.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -40,9 +44,11 @@ export class UserSelectMenuBuilder extends BaseSelectMenuBuilder<APIUserSelectCo
* @param users - The users to add
*/
public addDefaultUsers(...users: RestOrArray<Snowflake>) {
const normalizedValues = users.map((id) => {
return { id, type: SelectMenuDefaultValueType.User };
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.User>[];
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);
Expand All @@ -55,9 +61,11 @@ export class UserSelectMenuBuilder extends BaseSelectMenuBuilder<APIUserSelectCo
* @param users - The users to set
*/
public setDefaultUsers(...users: RestOrArray<Snowflake>) {
const normalizedValues = users.map((id) => {
return { id, type: SelectMenuDefaultValueType.User };
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.User>[];
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);
Expand Down

0 comments on commit 91dd431

Please sign in to comment.