|
| 1 | +import type { |
| 2 | + APIChannelSelectComponent, |
| 3 | + APILabelComponent, |
| 4 | + APIMentionableSelectComponent, |
| 5 | + APIRoleSelectComponent, |
| 6 | + APIStringSelectComponent, |
| 7 | + APITextInputComponent, |
| 8 | + APIUserSelectComponent, |
| 9 | +} from 'discord-api-types/v10'; |
| 10 | +import { ComponentType } from 'discord-api-types/v10'; |
| 11 | +import { ComponentBuilder } from '../Component.js'; |
| 12 | +import { createComponentBuilder, resolveBuilder } from '../Components.js'; |
| 13 | +import { ChannelSelectMenuBuilder } from '../selectMenu/ChannelSelectMenu.js'; |
| 14 | +import { MentionableSelectMenuBuilder } from '../selectMenu/MentionableSelectMenu.js'; |
| 15 | +import { RoleSelectMenuBuilder } from '../selectMenu/RoleSelectMenu.js'; |
| 16 | +import { StringSelectMenuBuilder } from '../selectMenu/StringSelectMenu.js'; |
| 17 | +import { UserSelectMenuBuilder } from '../selectMenu/UserSelectMenu.js'; |
| 18 | +import { TextInputBuilder } from '../textInput/TextInput.js'; |
| 19 | +import { labelPredicate } from './Assertions.js'; |
| 20 | + |
| 21 | +export interface LabelBuilderData extends Partial<Omit<APILabelComponent, 'component'>> { |
| 22 | + component?: |
| 23 | + | ChannelSelectMenuBuilder |
| 24 | + | MentionableSelectMenuBuilder |
| 25 | + | RoleSelectMenuBuilder |
| 26 | + | StringSelectMenuBuilder |
| 27 | + | TextInputBuilder |
| 28 | + | UserSelectMenuBuilder; |
| 29 | +} |
| 30 | + |
| 31 | +/** |
| 32 | + * A builder that creates API-compatible JSON data for labels. |
| 33 | + */ |
| 34 | +export class LabelBuilder extends ComponentBuilder<LabelBuilderData> { |
| 35 | + /** |
| 36 | + * @internal |
| 37 | + */ |
| 38 | + public override readonly data: LabelBuilderData; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a new label. |
| 42 | + * |
| 43 | + * @param data - The API data to create this label with |
| 44 | + * @example |
| 45 | + * Creating a label from an API data object: |
| 46 | + * ```ts |
| 47 | + * const label = new LabelBuilder({ |
| 48 | + * label: "label", |
| 49 | + * component, |
| 50 | + * }); |
| 51 | + * ``` |
| 52 | + * @example |
| 53 | + * Creating a label using setters and API data: |
| 54 | + * ```ts |
| 55 | + * const label = new LabelBuilder({ |
| 56 | + * label: 'label', |
| 57 | + * component, |
| 58 | + * }).setLabel('new text'); |
| 59 | + * ``` |
| 60 | + */ |
| 61 | + public constructor(data: Partial<APILabelComponent> = {}) { |
| 62 | + super({ type: ComponentType.Label }); |
| 63 | + |
| 64 | + const { component, ...rest } = data; |
| 65 | + |
| 66 | + this.data = { |
| 67 | + ...rest, |
| 68 | + component: component ? createComponentBuilder(component) : undefined, |
| 69 | + type: ComponentType.Label, |
| 70 | + }; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Sets the label for this label. |
| 75 | + * |
| 76 | + * @param label - The label to use |
| 77 | + */ |
| 78 | + public setLabel(label: string) { |
| 79 | + this.data.label = label; |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Sets the description for this label. |
| 85 | + * |
| 86 | + * @param description - The description to use |
| 87 | + */ |
| 88 | + public setDescription(description: string) { |
| 89 | + this.data.description = description; |
| 90 | + return this; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Clears the description for this label. |
| 95 | + */ |
| 96 | + public clearDescription() { |
| 97 | + this.data.description = undefined; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Sets a string select menu component to this label. |
| 103 | + * |
| 104 | + * @param input - A function that returns a component builder or an already built builder |
| 105 | + */ |
| 106 | + public setStringSelectMenuComponent( |
| 107 | + input: |
| 108 | + | APIStringSelectComponent |
| 109 | + | StringSelectMenuBuilder |
| 110 | + | ((builder: StringSelectMenuBuilder) => StringSelectMenuBuilder), |
| 111 | + ): this { |
| 112 | + this.data.component = resolveBuilder(input, StringSelectMenuBuilder); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Sets a user select menu component to this label. |
| 118 | + * |
| 119 | + * @param input - A function that returns a component builder or an already built builder |
| 120 | + */ |
| 121 | + public setUserSelectMenuComponent( |
| 122 | + input: APIUserSelectComponent | UserSelectMenuBuilder | ((builder: UserSelectMenuBuilder) => UserSelectMenuBuilder), |
| 123 | + ): this { |
| 124 | + this.data.component = resolveBuilder(input, UserSelectMenuBuilder); |
| 125 | + return this; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Sets a role select menu component to this label. |
| 130 | + * |
| 131 | + * @param input - A function that returns a component builder or an already built builder |
| 132 | + */ |
| 133 | + public setRoleSelectMenuComponent( |
| 134 | + input: APIRoleSelectComponent | RoleSelectMenuBuilder | ((builder: RoleSelectMenuBuilder) => RoleSelectMenuBuilder), |
| 135 | + ): this { |
| 136 | + this.data.component = resolveBuilder(input, RoleSelectMenuBuilder); |
| 137 | + return this; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Sets a mentionable select menu component to this label. |
| 142 | + * |
| 143 | + * @param input - A function that returns a component builder or an already built builder |
| 144 | + */ |
| 145 | + public setMentionableSelectMenuComponent( |
| 146 | + input: |
| 147 | + | APIMentionableSelectComponent |
| 148 | + | MentionableSelectMenuBuilder |
| 149 | + | ((builder: MentionableSelectMenuBuilder) => MentionableSelectMenuBuilder), |
| 150 | + ): this { |
| 151 | + this.data.component = resolveBuilder(input, MentionableSelectMenuBuilder); |
| 152 | + return this; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Sets a channel select menu component to this label. |
| 157 | + * |
| 158 | + * @param input - A function that returns a component builder or an already built builder |
| 159 | + */ |
| 160 | + public setChannelSelectMenuComponent( |
| 161 | + input: |
| 162 | + | APIChannelSelectComponent |
| 163 | + | ChannelSelectMenuBuilder |
| 164 | + | ((builder: ChannelSelectMenuBuilder) => ChannelSelectMenuBuilder), |
| 165 | + ): this { |
| 166 | + this.data.component = resolveBuilder(input, ChannelSelectMenuBuilder); |
| 167 | + return this; |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Sets a text input component to this label. |
| 172 | + * |
| 173 | + * @param input - A function that returns a component builder or an already built builder |
| 174 | + */ |
| 175 | + public setTextInputComponent( |
| 176 | + input: APITextInputComponent | TextInputBuilder | ((builder: TextInputBuilder) => TextInputBuilder), |
| 177 | + ): this { |
| 178 | + this.data.component = resolveBuilder(input, TextInputBuilder); |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * {@inheritDoc ComponentBuilder.toJSON} |
| 184 | + */ |
| 185 | + public override toJSON(): APILabelComponent { |
| 186 | + const { component, ...rest } = this.data; |
| 187 | + |
| 188 | + const data = { |
| 189 | + ...rest, |
| 190 | + // The label predicate validates the component. |
| 191 | + component: component?.toJSON(), |
| 192 | + }; |
| 193 | + |
| 194 | + labelPredicate.parse(data); |
| 195 | + |
| 196 | + return data as APILabelComponent; |
| 197 | + } |
| 198 | +} |
0 commit comments