feat: default select menu values (#9867)

* feat: default select menu values

* feat(Message): support

* fix: fix crashes when an array is supplied and remove assertion

* docs(transformResolved): `BaseChannel` is the correct type

* refactor: prefer assignment

* chore: export function again

* fix(Util): fix circular dependency

* refactor(MentionableSelectMenu): clone in method

* docs: remove semicolon

* feat(MentionableSelectMenu): add `addDefaultValues()`

* refactor: reduce overhead

* types: adjust `channel`

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
This commit is contained in:
Jaw0r3k
2023-11-12 17:32:41 +01:00
committed by GitHub
parent b5e23ec2ec
commit 4ff3ea4a1b
10 changed files with 320 additions and 73 deletions

View File

@@ -1,5 +1,12 @@
import type { APIMentionableSelectComponent } from 'discord-api-types/v10';
import { ComponentType } from 'discord-api-types/v10';
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';
/**
@@ -31,4 +38,79 @@ export class MentionableSelectMenuBuilder extends BaseSelectMenuBuilder<APIMenti
public constructor(data?: Partial<APIMentionableSelectComponent>) {
super({ ...data, type: ComponentType.MentionableSelect });
}
/**
* Adds default roles to this auto populated select menu.
*
* @param roles - The roles to add
*/
public addDefaultRoles(...roles: RestOrArray<Snowflake>) {
const normalizedValues = normalizeArray(roles);
optionsLengthValidator.parse((this.data.default_values?.length ?? 0) + normalizedValues.length);
this.data.default_values ??= [];
this.data.default_values.push(
...normalizedValues.map((id) => ({
id,
type: SelectMenuDefaultValueType.Role as const,
})),
);
return this;
}
/**
* Adds default users to this auto populated select menu.
*
* @param users - The users to add
*/
public addDefaultUsers(...users: RestOrArray<Snowflake>) {
const normalizedValues = normalizeArray(users);
optionsLengthValidator.parse((this.data.default_values?.length ?? 0) + normalizedValues.length);
this.data.default_values ??= [];
this.data.default_values.push(
...normalizedValues.map((id) => ({
id,
type: SelectMenuDefaultValueType.User as const,
})),
);
return this;
}
/**
* Adds default values to this auto populated select menu.
*
* @param values - The values to add
*/
public addDefaultValues(
...values: RestOrArray<
| APISelectMenuDefaultValue<SelectMenuDefaultValueType.Role>
| APISelectMenuDefaultValue<SelectMenuDefaultValueType.User>
>
) {
const normalizedValues = normalizeArray(values);
optionsLengthValidator.parse((this.data.default_values?.length ?? 0) + normalizedValues.length);
this.data.default_values ??= [];
this.data.default_values.push(...normalizedValues);
return this;
}
/**
* Sets default values to this auto populated select menu.
*
* @param values - The values to set
*/
public setDefaultValues(
...values: RestOrArray<
| APISelectMenuDefaultValue<SelectMenuDefaultValueType.Role>
| APISelectMenuDefaultValue<SelectMenuDefaultValueType.User>
>
) {
const normalizedValues = normalizeArray(values);
optionsLengthValidator.parse(normalizedValues.length);
this.data.default_values = normalizedValues.slice();
return this;
}
}