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,11 @@
import type { APIUserSelectComponent } from 'discord-api-types/v10';
import { ComponentType } from 'discord-api-types/v10';
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';
/**
@@ -31,4 +37,41 @@ export class UserSelectMenuBuilder extends BaseSelectMenuBuilder<APIUserSelectCo
public constructor(data?: Partial<APIUserSelectComponent>) {
super({ ...data, type: ComponentType.UserSelect });
}
/**
* 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;
}
/**
* Sets default users to this auto populated select menu.
*
* @param users - The users to set
*/
public setDefaultUsers(...users: RestOrArray<Snowflake>) {
const normalizedValues = normalizeArray(users);
optionsLengthValidator.parse(normalizedValues.length);
this.data.default_values = normalizedValues.map((id) => ({
id,
type: SelectMenuDefaultValueType.User as const,
}));
return this;
}
}