mirror of
https://github.com/discordjs/discord.js.git
synced 2026-06-01 08:30:08 +00:00
90 lines
2.4 KiB
TypeScript
90 lines
2.4 KiB
TypeScript
import {
|
|
type APIRoleSelectComponent,
|
|
type Snowflake,
|
|
ComponentType,
|
|
SelectMenuDefaultValueType,
|
|
} from 'discord-api-types/v10';
|
|
import { type RestOrArray, normalizeArray } from '../../util/normalizeArray.js';
|
|
import { validate } from '../../util/validation.js';
|
|
import { selectMenuRolePredicate } from '../Assertions.js';
|
|
import { BaseSelectMenuBuilder } from './BaseSelectMenu.js';
|
|
|
|
/**
|
|
* A builder that creates API-compatible JSON data for role select menus.
|
|
*/
|
|
export class RoleSelectMenuBuilder extends BaseSelectMenuBuilder<APIRoleSelectComponent> {
|
|
protected override readonly data: Partial<APIRoleSelectComponent>;
|
|
|
|
/**
|
|
* Creates a new select menu from API data.
|
|
*
|
|
* @param data - The API data to create this select menu with
|
|
* @example
|
|
* Creating a select menu from an API data object:
|
|
* ```ts
|
|
* const selectMenu = new RoleSelectMenuBuilder({
|
|
* custom_id: 'a cool select menu',
|
|
* placeholder: 'select an option',
|
|
* max_values: 2,
|
|
* });
|
|
* ```
|
|
* @example
|
|
* Creating a select menu using setters and API data:
|
|
* ```ts
|
|
* const selectMenu = new RoleSelectMenuBuilder({
|
|
* custom_id: 'a cool select menu',
|
|
* })
|
|
* .setMinValues(1);
|
|
* ```
|
|
*/
|
|
public constructor(data: Partial<APIRoleSelectComponent> = {}) {
|
|
super();
|
|
this.data = { ...structuredClone(data), type: ComponentType.RoleSelect };
|
|
}
|
|
|
|
/**
|
|
* Adds default roles to this auto populated select menu.
|
|
*
|
|
* @param roles - The roles to add
|
|
*/
|
|
public addDefaultRoles(...roles: RestOrArray<Snowflake>) {
|
|
const normalizedValues = normalizeArray(roles);
|
|
this.data.default_values ??= [];
|
|
|
|
this.data.default_values.push(
|
|
...normalizedValues.map((id) => ({
|
|
id,
|
|
type: SelectMenuDefaultValueType.Role as const,
|
|
})),
|
|
);
|
|
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Sets default roles for this auto populated select menu.
|
|
*
|
|
* @param roles - The roles to set
|
|
*/
|
|
public setDefaultRoles(...roles: RestOrArray<Snowflake>) {
|
|
const normalizedValues = normalizeArray(roles);
|
|
|
|
this.data.default_values = normalizedValues.map((id) => ({
|
|
id,
|
|
type: SelectMenuDefaultValueType.Role as const,
|
|
}));
|
|
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* {@inheritDoc ComponentBuilder.toJSON}
|
|
*/
|
|
public override toJSON(validationOverride?: boolean): APIRoleSelectComponent {
|
|
const clone = structuredClone(this.data);
|
|
validate(selectMenuRolePredicate, clone, validationOverride);
|
|
|
|
return clone as APIRoleSelectComponent;
|
|
}
|
|
}
|