Skip to content

Commit fe5b6b0

Browse files
committed
fix: fix crashes when an array is supplied and remove assertion
1 parent 045110b commit fe5b6b0

File tree

4 files changed

+67
-41
lines changed

4 files changed

+67
-41
lines changed

packages/builders/src/components/selectMenu/ChannelSelectMenu.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import type {
2-
APIChannelSelectComponent,
3-
APISelectMenuDefaultValue,
4-
ChannelType,
5-
Snowflake,
1+
import {
2+
type APIChannelSelectComponent,
3+
type ChannelType,
4+
type Snowflake,
5+
ComponentType,
6+
SelectMenuDefaultValueType,
67
} from 'discord-api-types/v10';
7-
import { ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10';
8-
import { normalizeArray, type RestOrArray } from '../../util/normalizeArray.js';
8+
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
99
import { channelTypesValidator, customIdValidator, optionsLengthValidator } from '../Assertions.js';
1010
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
1111

@@ -70,9 +70,11 @@ export class ChannelSelectMenuBuilder extends BaseSelectMenuBuilder<APIChannelSe
7070
* @param channels - The channels to add
7171
*/
7272
public addDefaultChannels(...channels: RestOrArray<Snowflake>) {
73-
const normalizedValues = channels.map((id) => {
74-
return { id, type: SelectMenuDefaultValueType.Channel };
75-
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Channel>[];
73+
const normalizedValues = normalizeArray(channels).map((id) => ({
74+
id,
75+
type: SelectMenuDefaultValueType.Channel as const,
76+
}));
77+
7678
this.data.default_values ??= [];
7779
optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length);
7880
this.data.default_values.push(...normalizedValues);
@@ -85,9 +87,11 @@ export class ChannelSelectMenuBuilder extends BaseSelectMenuBuilder<APIChannelSe
8587
* @param channels - The channels to set
8688
*/
8789
public setDefaultChannels(...channels: RestOrArray<Snowflake>) {
88-
const normalizedValues = channels.map((id) => {
89-
return { id, type: SelectMenuDefaultValueType.Channel };
90-
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Channel>[];
90+
const normalizedValues = normalizeArray(channels).map((id) => ({
91+
id,
92+
type: SelectMenuDefaultValueType.Channel as const,
93+
}));
94+
9195
optionsLengthValidator.parse(normalizedValues.length);
9296
this.data.default_values ??= [];
9397
this.data.default_values.splice(0, this.data.default_values.length, ...normalizedValues);

packages/builders/src/components/selectMenu/MentionableSelectMenu.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
import type { APIMentionableSelectComponent, APISelectMenuDefaultValue, Snowflake } from 'discord-api-types/v10';
2-
import { ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10';
3-
import type { RestOrArray } from '../../index.js';
4-
import { normalizeArray } from '../../index.js';
1+
import {
2+
type APIMentionableSelectComponent,
3+
type APISelectMenuDefaultValue,
4+
type Snowflake,
5+
ComponentType,
6+
SelectMenuDefaultValueType,
7+
} from 'discord-api-types/v10';
8+
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
59
import { optionsLengthValidator } from '../Assertions.js';
610
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
711

@@ -41,9 +45,11 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder<APIMenti
4145
* @param roles - The roles to add
4246
*/
4347
public addDefaultRoles(...roles: RestOrArray<Snowflake>) {
44-
const normalizedValues = roles.map((id) => {
45-
return { id, type: SelectMenuDefaultValueType.Role };
46-
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Role>[];
48+
const normalizedValues = normalizeArray(roles).map((id) => ({
49+
id,
50+
type: SelectMenuDefaultValueType.Role as const,
51+
}));
52+
4753
this.data.default_values ??= [];
4854
optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length);
4955
this.data.default_values.push(...normalizedValues);
@@ -56,9 +62,11 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder<APIMenti
5662
* @param users - The users to add
5763
*/
5864
public addDefaultUsers(...users: RestOrArray<Snowflake>) {
59-
const normalizedValues = users.map((id) => {
60-
return { id, type: SelectMenuDefaultValueType.User };
61-
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.User>[];
65+
const normalizedValues = normalizeArray(users).map((id) => ({
66+
id,
67+
type: SelectMenuDefaultValueType.User as const,
68+
}));
69+
6270
this.data.default_values ??= [];
6371
optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length);
6472
this.data.default_values.push(...normalizedValues);

packages/builders/src/components/selectMenu/RoleSelectMenu.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import type { APIRoleSelectComponent, APISelectMenuDefaultValue, Snowflake } from 'discord-api-types/v10';
2-
import { ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10';
3-
import type { RestOrArray } from '../../index.js';
1+
import {
2+
type APIRoleSelectComponent,
3+
type Snowflake,
4+
ComponentType,
5+
SelectMenuDefaultValueType,
6+
} from 'discord-api-types/v10';
7+
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
48
import { optionsLengthValidator } from '../Assertions.js';
59
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
610

@@ -40,9 +44,11 @@ export class RoleSelectMenuBuilder extends BaseSelectMenuBuilder<APIRoleSelectCo
4044
* @param roles - The roles to add
4145
*/
4246
public addDefaultRoles(...roles: RestOrArray<Snowflake>) {
43-
const normalizedValues = roles.map((id) => {
44-
return { id, type: SelectMenuDefaultValueType.Role };
45-
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Role>[];
47+
const normalizedValues = normalizeArray(roles).map((id) => ({
48+
id,
49+
type: SelectMenuDefaultValueType.Role as const,
50+
}));
51+
4652
this.data.default_values ??= [];
4753
optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length);
4854
this.data.default_values.push(...normalizedValues);
@@ -55,9 +61,11 @@ export class RoleSelectMenuBuilder extends BaseSelectMenuBuilder<APIRoleSelectCo
5561
* @param roles - The roles to set
5662
*/
5763
public setDefaultRoles(...roles: RestOrArray<Snowflake>) {
58-
const normalizedValues = roles.map((id) => {
59-
return { id, type: SelectMenuDefaultValueType.Role };
60-
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.Role>[];
64+
const normalizedValues = normalizeArray(roles).map((id) => ({
65+
id,
66+
type: SelectMenuDefaultValueType.Role as const,
67+
}));
68+
6169
optionsLengthValidator.parse(normalizedValues.length);
6270
this.data.default_values ??= [];
6371
this.data.default_values.splice(0, this.data.default_values.length, ...normalizedValues);

packages/builders/src/components/selectMenu/UserSelectMenu.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
import type { APISelectMenuDefaultValue, APIUserSelectComponent, Snowflake } from 'discord-api-types/v10';
2-
import { ComponentType, SelectMenuDefaultValueType } from 'discord-api-types/v10';
3-
import type { RestOrArray } from '../../index.js';
1+
import {
2+
type APIUserSelectComponent,
3+
type Snowflake,
4+
ComponentType,
5+
SelectMenuDefaultValueType,
6+
} from 'discord-api-types/v10';
7+
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
48
import { optionsLengthValidator } from '../Assertions.js';
59
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
610

@@ -40,9 +44,10 @@ export class UserSelectMenuBuilder extends BaseSelectMenuBuilder<APIUserSelectCo
4044
* @param users - The users to add
4145
*/
4246
public addDefaultUsers(...users: RestOrArray<Snowflake>) {
43-
const normalizedValues = users.map((id) => {
44-
return { id, type: SelectMenuDefaultValueType.User };
45-
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.User>[];
47+
const normalizedValues = normalizeArray(users).map((id) => ({
48+
id,
49+
type: SelectMenuDefaultValueType.User as const,
50+
}));
4651
this.data.default_values ??= [];
4752
optionsLengthValidator.parse(this.data.default_values.length + normalizedValues.length);
4853
this.data.default_values.push(...normalizedValues);
@@ -55,9 +60,10 @@ export class UserSelectMenuBuilder extends BaseSelectMenuBuilder<APIUserSelectCo
5560
* @param users - The users to set
5661
*/
5762
public setDefaultUsers(...users: RestOrArray<Snowflake>) {
58-
const normalizedValues = users.map((id) => {
59-
return { id, type: SelectMenuDefaultValueType.User };
60-
}) as APISelectMenuDefaultValue<SelectMenuDefaultValueType.User>[];
63+
const normalizedValues = normalizeArray(users).map((id) => ({
64+
id,
65+
type: SelectMenuDefaultValueType.User as const,
66+
}));
6167
optionsLengthValidator.parse(normalizedValues.length);
6268
this.data.default_values ??= [];
6369
this.data.default_values.splice(0, this.data.default_values.length, ...normalizedValues);

0 commit comments

Comments
 (0)