mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-02 17:10:08 +00:00
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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user