UNPKG

@discordjs/builders

Version:

A set of builders that you can use when creating your bot

1,660 lines (1,628 loc) • 112 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __decorateClass = (decorators, target, key, kind) => { var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target; for (var i = decorators.length - 1, decorator; i >= 0; i--) if (decorator = decorators[i]) result = (kind ? decorator(target, key, result) : decorator(result)) || result; if (kind && result) __defProp(target, key, result); return result; }; // src/messages/embed/Assertions.ts var Assertions_exports = {}; __export(Assertions_exports, { RGBPredicate: () => RGBPredicate, authorNamePredicate: () => authorNamePredicate, colorPredicate: () => colorPredicate, descriptionPredicate: () => descriptionPredicate, embedAuthorPredicate: () => embedAuthorPredicate, embedFieldPredicate: () => embedFieldPredicate, embedFieldsArrayPredicate: () => embedFieldsArrayPredicate, embedFooterPredicate: () => embedFooterPredicate, fieldInlinePredicate: () => fieldInlinePredicate, fieldLengthPredicate: () => fieldLengthPredicate, fieldNamePredicate: () => fieldNamePredicate, fieldValuePredicate: () => fieldValuePredicate, footerTextPredicate: () => footerTextPredicate, imageURLPredicate: () => imageURLPredicate, timestampPredicate: () => timestampPredicate, titlePredicate: () => titlePredicate, urlPredicate: () => urlPredicate, validateFieldLength: () => validateFieldLength }); import { s } from "@sapphire/shapeshift"; // src/util/validation.ts var validate = true; function enableValidators() { return validate = true; } __name(enableValidators, "enableValidators"); function disableValidators() { return validate = false; } __name(disableValidators, "disableValidators"); function isValidationEnabled() { return validate; } __name(isValidationEnabled, "isValidationEnabled"); // src/messages/embed/Assertions.ts var fieldNamePredicate = s.string().lengthLessThanOrEqual(256).setValidationEnabled(isValidationEnabled); var fieldValuePredicate = s.string().lengthLessThanOrEqual(1024).setValidationEnabled(isValidationEnabled); var fieldInlinePredicate = s.boolean().optional(); var embedFieldPredicate = s.object({ name: fieldNamePredicate, value: fieldValuePredicate, inline: fieldInlinePredicate }).setValidationEnabled(isValidationEnabled); var embedFieldsArrayPredicate = embedFieldPredicate.array().setValidationEnabled(isValidationEnabled); var fieldLengthPredicate = s.number().lessThanOrEqual(25).setValidationEnabled(isValidationEnabled); function validateFieldLength(amountAdding, fields) { fieldLengthPredicate.parse((fields?.length ?? 0) + amountAdding); } __name(validateFieldLength, "validateFieldLength"); var authorNamePredicate = fieldNamePredicate.lengthGreaterThanOrEqual(1).nullable().setValidationEnabled(isValidationEnabled); var imageURLPredicate = s.string().url({ allowedProtocols: ["http:", "https:", "attachment:"] }).nullish().setValidationEnabled(isValidationEnabled); var urlPredicate = s.string().url({ allowedProtocols: ["http:", "https:"] }).nullish().setValidationEnabled(isValidationEnabled); var embedAuthorPredicate = s.object({ name: authorNamePredicate, iconURL: imageURLPredicate, url: urlPredicate }).setValidationEnabled(isValidationEnabled); var RGBPredicate = s.number().int().greaterThanOrEqual(0).lessThanOrEqual(255).setValidationEnabled(isValidationEnabled); var colorPredicate = s.number().int().greaterThanOrEqual(0).lessThanOrEqual(16777215).or(s.tuple([RGBPredicate, RGBPredicate, RGBPredicate])).nullable().setValidationEnabled(isValidationEnabled); var descriptionPredicate = s.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(4096).nullable().setValidationEnabled(isValidationEnabled); var footerTextPredicate = s.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(2048).nullable().setValidationEnabled(isValidationEnabled); var embedFooterPredicate = s.object({ text: footerTextPredicate, iconURL: imageURLPredicate }).setValidationEnabled(isValidationEnabled); var timestampPredicate = s.union([s.number(), s.date()]).nullable().setValidationEnabled(isValidationEnabled); var titlePredicate = fieldNamePredicate.lengthGreaterThanOrEqual(1).nullable().setValidationEnabled(isValidationEnabled); // src/util/normalizeArray.ts function normalizeArray(arr) { if (Array.isArray(arr[0])) return [...arr[0]]; return arr; } __name(normalizeArray, "normalizeArray"); // src/messages/embed/Embed.ts var EmbedBuilder = class { static { __name(this, "EmbedBuilder"); } /** * The API data associated with this embed. */ data; /** * Creates a new embed from API data. * * @param data - The API data to create this embed with */ constructor(data = {}) { this.data = { ...data }; if (data.timestamp) this.data.timestamp = new Date(data.timestamp).toISOString(); } /** * Appends fields to the embed. * * @remarks * This method accepts either an array of fields or a variable number of field parameters. * The maximum amount of fields that can be added is 25. * @example * Using an array: * ```ts * const fields: APIEmbedField[] = ...; * const embed = new EmbedBuilder() * .addFields(fields); * ``` * @example * Using rest parameters (variadic): * ```ts * const embed = new EmbedBuilder() * .addFields( * { name: 'Field 1', value: 'Value 1' }, * { name: 'Field 2', value: 'Value 2' }, * ); * ``` * @param fields - The fields to add */ addFields(...fields) { const normalizedFields = normalizeArray(fields); validateFieldLength(normalizedFields.length, this.data.fields); embedFieldsArrayPredicate.parse(normalizedFields); if (this.data.fields) this.data.fields.push(...normalizedFields); else this.data.fields = normalizedFields; return this; } /** * Removes, replaces, or inserts fields for this embed. * * @remarks * This method behaves similarly * to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice | Array.prototype.splice()}. * The maximum amount of fields that can be added is 25. * * It's useful for modifying and adjusting order of the already-existing fields of an embed. * @example * Remove the first field: * ```ts * embed.spliceFields(0, 1); * ``` * @example * Remove the first n fields: * ```ts * const n = 4; * embed.spliceFields(0, n); * ``` * @example * Remove the last field: * ```ts * embed.spliceFields(-1, 1); * ``` * @param index - The index to start at * @param deleteCount - The number of fields to remove * @param fields - The replacing field objects */ spliceFields(index, deleteCount, ...fields) { validateFieldLength(fields.length - deleteCount, this.data.fields); embedFieldsArrayPredicate.parse(fields); if (this.data.fields) this.data.fields.splice(index, deleteCount, ...fields); else this.data.fields = fields; return this; } /** * Sets the fields for this embed. * * @remarks * This method is an alias for {@link EmbedBuilder.spliceFields}. More specifically, * it splices the entire array of fields, replacing them with the provided fields. * * You can set a maximum of 25 fields. * @param fields - The fields to set */ setFields(...fields) { this.spliceFields(0, this.data.fields?.length ?? 0, ...normalizeArray(fields)); return this; } /** * Sets the author of this embed. * * @param options - The options to use */ setAuthor(options) { if (options === null) { this.data.author = void 0; return this; } embedAuthorPredicate.parse(options); this.data.author = { name: options.name, url: options.url, icon_url: options.iconURL }; return this; } /** * Sets the color of this embed. * * @param color - The color to use */ setColor(color) { colorPredicate.parse(color); if (Array.isArray(color)) { const [red, green, blue] = color; this.data.color = (red << 16) + (green << 8) + blue; return this; } this.data.color = color ?? void 0; return this; } /** * Sets the description of this embed. * * @param description - The description to use */ setDescription(description) { descriptionPredicate.parse(description); this.data.description = description ?? void 0; return this; } /** * Sets the footer of this embed. * * @param options - The footer to use */ setFooter(options) { if (options === null) { this.data.footer = void 0; return this; } embedFooterPredicate.parse(options); this.data.footer = { text: options.text, icon_url: options.iconURL }; return this; } /** * Sets the image of this embed. * * @param url - The image URL to use */ setImage(url) { imageURLPredicate.parse(url); this.data.image = url ? { url } : void 0; return this; } /** * Sets the thumbnail of this embed. * * @param url - The thumbnail URL to use */ setThumbnail(url) { imageURLPredicate.parse(url); this.data.thumbnail = url ? { url } : void 0; return this; } /** * Sets the timestamp of this embed. * * @param timestamp - The timestamp or date to use */ setTimestamp(timestamp = Date.now()) { timestampPredicate.parse(timestamp); this.data.timestamp = timestamp ? new Date(timestamp).toISOString() : void 0; return this; } /** * Sets the title for this embed. * * @param title - The title to use */ setTitle(title) { titlePredicate.parse(title); this.data.title = title ?? void 0; return this; } /** * Sets the URL of this embed. * * @param url - The URL to use */ setURL(url) { urlPredicate.parse(url); this.data.url = url ?? void 0; return this; } /** * Serializes this builder to API-compatible JSON data. * * @remarks * This method runs validations on the data before serializing it. * As such, it may throw an error if the data is invalid. */ toJSON() { return { ...this.data }; } }; // src/index.ts export * from "@discordjs/formatters"; // src/components/Assertions.ts var Assertions_exports2 = {}; __export(Assertions_exports2, { buttonLabelValidator: () => buttonLabelValidator, buttonStyleValidator: () => buttonStyleValidator, channelTypesValidator: () => channelTypesValidator, customIdValidator: () => customIdValidator, defaultValidator: () => defaultValidator, disabledValidator: () => disabledValidator, emojiValidator: () => emojiValidator, idValidator: () => idValidator, jsonOptionValidator: () => jsonOptionValidator, labelValueDescriptionValidator: () => labelValueDescriptionValidator, minMaxValidator: () => minMaxValidator, optionValidator: () => optionValidator, optionsLengthValidator: () => optionsLengthValidator, optionsValidator: () => optionsValidator, placeholderValidator: () => placeholderValidator, urlValidator: () => urlValidator, validateRequiredButtonParameters: () => validateRequiredButtonParameters, validateRequiredSelectMenuOptionParameters: () => validateRequiredSelectMenuOptionParameters, validateRequiredSelectMenuParameters: () => validateRequiredSelectMenuParameters }); import { s as s2 } from "@sapphire/shapeshift"; import { ButtonStyle, ChannelType } from "discord-api-types/v10"; // src/components/selectMenu/StringSelectMenuOption.ts var StringSelectMenuOptionBuilder = class { /** * Creates a new string select menu option from API data. * * @param data - The API data to create this string select menu option with * @example * Creating a string select menu option from an API data object: * ```ts * const selectMenuOption = new SelectMenuOptionBuilder({ * label: 'catchy label', * value: '1', * }); * ``` * @example * Creating a string select menu option using setters and API data: * ```ts * const selectMenuOption = new SelectMenuOptionBuilder({ * default: true, * value: '1', * }) * .setLabel('woah'); * ``` */ constructor(data = {}) { this.data = data; } static { __name(this, "StringSelectMenuOptionBuilder"); } /** * Sets the label for this option. * * @param label - The label to use */ setLabel(label) { this.data.label = labelValueDescriptionValidator.parse(label); return this; } /** * Sets the value for this option. * * @param value - The value to use */ setValue(value) { this.data.value = labelValueDescriptionValidator.parse(value); return this; } /** * Sets the description for this option. * * @param description - The description to use */ setDescription(description) { this.data.description = labelValueDescriptionValidator.parse(description); return this; } /** * Sets whether this option is selected by default. * * @param isDefault - Whether this option is selected by default */ setDefault(isDefault = true) { this.data.default = defaultValidator.parse(isDefault); return this; } /** * Sets the emoji to display for this option. * * @param emoji - The emoji to use */ setEmoji(emoji) { this.data.emoji = emojiValidator.parse(emoji); return this; } /** * {@inheritDoc BaseSelectMenuBuilder.toJSON} */ toJSON() { validateRequiredSelectMenuOptionParameters(this.data.label, this.data.value); return { ...this.data }; } }; // src/components/Assertions.ts var idValidator = s2.number().safeInt().greaterThanOrEqual(1).lessThan(4294967296).setValidationEnabled(isValidationEnabled); var customIdValidator = s2.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(100).setValidationEnabled(isValidationEnabled); var emojiValidator = s2.object({ id: s2.string(), name: s2.string(), animated: s2.boolean() }).partial().strict().setValidationEnabled(isValidationEnabled); var disabledValidator = s2.boolean(); var buttonLabelValidator = s2.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(80).setValidationEnabled(isValidationEnabled); var buttonStyleValidator = s2.nativeEnum(ButtonStyle); var placeholderValidator = s2.string().lengthLessThanOrEqual(150).setValidationEnabled(isValidationEnabled); var minMaxValidator = s2.number().int().greaterThanOrEqual(0).lessThanOrEqual(25).setValidationEnabled(isValidationEnabled); var labelValueDescriptionValidator = s2.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(100).setValidationEnabled(isValidationEnabled); var jsonOptionValidator = s2.object({ label: labelValueDescriptionValidator, value: labelValueDescriptionValidator, description: labelValueDescriptionValidator.optional(), emoji: emojiValidator.optional(), default: s2.boolean().optional() }).setValidationEnabled(isValidationEnabled); var optionValidator = s2.instance(StringSelectMenuOptionBuilder).setValidationEnabled(isValidationEnabled); var optionsValidator = optionValidator.array().lengthGreaterThanOrEqual(0).setValidationEnabled(isValidationEnabled); var optionsLengthValidator = s2.number().int().greaterThanOrEqual(0).lessThanOrEqual(25).setValidationEnabled(isValidationEnabled); function validateRequiredSelectMenuParameters(options, customId) { customIdValidator.parse(customId); optionsValidator.parse(options); } __name(validateRequiredSelectMenuParameters, "validateRequiredSelectMenuParameters"); var defaultValidator = s2.boolean(); function validateRequiredSelectMenuOptionParameters(label, value) { labelValueDescriptionValidator.parse(label); labelValueDescriptionValidator.parse(value); } __name(validateRequiredSelectMenuOptionParameters, "validateRequiredSelectMenuOptionParameters"); var channelTypesValidator = s2.nativeEnum(ChannelType).array().setValidationEnabled(isValidationEnabled); var urlValidator = s2.string().url({ allowedProtocols: ["http:", "https:", "discord:"] }).setValidationEnabled(isValidationEnabled); function validateRequiredButtonParameters(style, label, emoji, customId, skuId, url) { if (style === ButtonStyle.Premium) { if (!skuId) { throw new RangeError("Premium buttons must have an SKU id."); } if (customId || label || url || emoji) { throw new RangeError("Premium buttons cannot have a custom id, label, URL, or emoji."); } } else { if (skuId) { throw new RangeError("Non-premium buttons must not have an SKU id."); } if (url && customId) { throw new RangeError("URL and custom id are mutually exclusive."); } if (!label && !emoji) { throw new RangeError("Non-premium buttons must have a label and/or an emoji."); } if (style === ButtonStyle.Link) { if (!url) { throw new RangeError("Link buttons must have a URL."); } } else if (url) { throw new RangeError("Non-premium and non-link buttons cannot have a URL."); } } } __name(validateRequiredButtonParameters, "validateRequiredButtonParameters"); // src/components/ActionRow.ts import { ComponentType as ComponentType16 } from "discord-api-types/v10"; // src/components/Component.ts var ComponentBuilder = class { static { __name(this, "ComponentBuilder"); } /** * The API data associated with this component. */ data; /** * Constructs a new kind of component. * * @param data - The data to construct a component out of */ constructor(data) { this.data = data; } /** * Sets the id (not the custom id) for this component. * * @param id - The id for this component */ setId(id) { this.data.id = idValidator.parse(id); return this; } /** * Clears the id of this component, defaulting to a default incremented id. */ clearId() { this.data.id = void 0; return this; } }; // src/components/Components.ts import { ComponentType as ComponentType15 } from "discord-api-types/v10"; // src/components/button/Button.ts import { ComponentType } from "discord-api-types/v10"; var ButtonBuilder = class extends ComponentBuilder { static { __name(this, "ButtonBuilder"); } /** * Creates a new button from API data. * * @param data - The API data to create this button with * @example * Creating a button from an API data object: * ```ts * const button = new ButtonBuilder({ * custom_id: 'a cool button', * style: ButtonStyle.Primary, * label: 'Click Me', * emoji: { * name: 'smile', * id: '123456789012345678', * }, * }); * ``` * @example * Creating a button using setters and API data: * ```ts * const button = new ButtonBuilder({ * style: ButtonStyle.Secondary, * label: 'Click Me', * }) * .setEmoji({ name: '🙂' }) * .setCustomId('another cool button'); * ``` */ constructor(data) { super({ type: ComponentType.Button, ...data }); } /** * Sets the style of this button. * * @param style - The style to use */ setStyle(style) { this.data.style = buttonStyleValidator.parse(style); return this; } /** * Sets the URL for this button. * * @remarks * This method is only available to buttons using the `Link` button style. * Only three types of URL schemes are currently supported: `https://`, `http://`, and `discord://`. * @param url - The URL to use */ setURL(url) { this.data.url = urlValidator.parse(url); return this; } /** * Sets the custom id for this button. * * @remarks * This method is only applicable to buttons that are not using the `Link` button style. * @param customId - The custom id to use */ setCustomId(customId) { this.data.custom_id = customIdValidator.parse(customId); return this; } /** * Sets the SKU id that represents a purchasable SKU for this button. * * @remarks Only available when using premium-style buttons. * @param skuId - The SKU id to use */ setSKUId(skuId) { this.data.sku_id = skuId; return this; } /** * Sets the emoji to display on this button. * * @param emoji - The emoji to use */ setEmoji(emoji) { this.data.emoji = emojiValidator.parse(emoji); return this; } /** * Sets whether this button is disabled. * * @param disabled - Whether to disable this button */ setDisabled(disabled = true) { this.data.disabled = disabledValidator.parse(disabled); return this; } /** * Sets the label for this button. * * @param label - The label to use */ setLabel(label) { this.data.label = buttonLabelValidator.parse(label); return this; } /** * {@inheritDoc ComponentBuilder.toJSON} */ toJSON() { validateRequiredButtonParameters( this.data.style, this.data.label, this.data.emoji, this.data.custom_id, this.data.sku_id, this.data.url ); return { ...this.data }; } }; // src/components/selectMenu/ChannelSelectMenu.ts import { ComponentType as ComponentType2, SelectMenuDefaultValueType } from "discord-api-types/v10"; // src/components/selectMenu/BaseSelectMenu.ts var BaseSelectMenuBuilder = class extends ComponentBuilder { static { __name(this, "BaseSelectMenuBuilder"); } /** * Sets the placeholder for this select menu. * * @param placeholder - The placeholder to use */ setPlaceholder(placeholder) { this.data.placeholder = placeholderValidator.parse(placeholder); return this; } /** * Sets the minimum values that must be selected in the select menu. * * @param minValues - The minimum values that must be selected */ setMinValues(minValues) { this.data.min_values = minMaxValidator.parse(minValues); return this; } /** * Sets the maximum values that must be selected in the select menu. * * @param maxValues - The maximum values that must be selected */ setMaxValues(maxValues) { this.data.max_values = minMaxValidator.parse(maxValues); return this; } /** * Sets the custom id for this select menu. * * @param customId - The custom id to use */ setCustomId(customId) { this.data.custom_id = customIdValidator.parse(customId); return this; } /** * Sets whether this select menu is disabled. * * @param disabled - Whether this select menu is disabled */ setDisabled(disabled = true) { this.data.disabled = disabledValidator.parse(disabled); return this; } /** * {@inheritDoc ComponentBuilder.toJSON} */ toJSON() { customIdValidator.parse(this.data.custom_id); return { ...this.data }; } }; // src/components/selectMenu/ChannelSelectMenu.ts var ChannelSelectMenuBuilder = class extends BaseSelectMenuBuilder { static { __name(this, "ChannelSelectMenuBuilder"); } /** * 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 ChannelSelectMenuBuilder({ * 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 ChannelSelectMenuBuilder({ * custom_id: 'a cool select menu', * }) * .addChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement) * .setMinValues(2); * ``` */ constructor(data) { super({ ...data, type: ComponentType2.ChannelSelect }); } /** * Adds channel types to this select menu. * * @param types - The channel types to use */ addChannelTypes(...types) { const normalizedTypes = normalizeArray(types); this.data.channel_types ??= []; this.data.channel_types.push(...channelTypesValidator.parse(normalizedTypes)); return this; } /** * Sets channel types for this select menu. * * @param types - The channel types to use */ setChannelTypes(...types) { const normalizedTypes = normalizeArray(types); this.data.channel_types ??= []; this.data.channel_types.splice(0, this.data.channel_types.length, ...channelTypesValidator.parse(normalizedTypes)); return this; } /** * Adds default channels to this auto populated select menu. * * @param channels - The channels to add */ addDefaultChannels(...channels) { const normalizedValues = normalizeArray(channels); 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.Channel })) ); return this; } /** * Sets default channels for this auto populated select menu. * * @param channels - The channels to set */ setDefaultChannels(...channels) { const normalizedValues = normalizeArray(channels); optionsLengthValidator.parse(normalizedValues.length); this.data.default_values = normalizedValues.map((id) => ({ id, type: SelectMenuDefaultValueType.Channel })); return this; } /** * {@inheritDoc BaseSelectMenuBuilder.toJSON} */ toJSON() { customIdValidator.parse(this.data.custom_id); return { ...this.data }; } }; // src/components/selectMenu/MentionableSelectMenu.ts import { ComponentType as ComponentType3, SelectMenuDefaultValueType as SelectMenuDefaultValueType2 } from "discord-api-types/v10"; var MentionableSelectMenuBuilder = class extends BaseSelectMenuBuilder { static { __name(this, "MentionableSelectMenuBuilder"); } /** * 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 MentionableSelectMenuBuilder({ * 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 MentionableSelectMenuBuilder({ * custom_id: 'a cool select menu', * }) * .setMinValues(1); * ``` */ constructor(data) { super({ ...data, type: ComponentType3.MentionableSelect }); } /** * Adds default roles to this auto populated select menu. * * @param roles - The roles to add */ addDefaultRoles(...roles) { 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: SelectMenuDefaultValueType2.Role })) ); return this; } /** * Adds default users to this auto populated select menu. * * @param users - The users to add */ addDefaultUsers(...users) { 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: SelectMenuDefaultValueType2.User })) ); return this; } /** * Adds default values to this auto populated select menu. * * @param values - The values to add */ addDefaultValues(...values) { 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 for this auto populated select menu. * * @param values - The values to set */ setDefaultValues(...values) { const normalizedValues = normalizeArray(values); optionsLengthValidator.parse(normalizedValues.length); this.data.default_values = normalizedValues; return this; } }; // src/components/selectMenu/RoleSelectMenu.ts import { ComponentType as ComponentType4, SelectMenuDefaultValueType as SelectMenuDefaultValueType3 } from "discord-api-types/v10"; var RoleSelectMenuBuilder = class extends BaseSelectMenuBuilder { static { __name(this, "RoleSelectMenuBuilder"); } /** * 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); * ``` */ constructor(data) { super({ ...data, type: ComponentType4.RoleSelect }); } /** * Adds default roles to this auto populated select menu. * * @param roles - The roles to add */ addDefaultRoles(...roles) { 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: SelectMenuDefaultValueType3.Role })) ); return this; } /** * Sets default roles for this auto populated select menu. * * @param roles - The roles to set */ setDefaultRoles(...roles) { const normalizedValues = normalizeArray(roles); optionsLengthValidator.parse(normalizedValues.length); this.data.default_values = normalizedValues.map((id) => ({ id, type: SelectMenuDefaultValueType3.Role })); return this; } }; // src/components/selectMenu/StringSelectMenu.ts import { ComponentType as ComponentType5 } from "discord-api-types/v10"; var StringSelectMenuBuilder = class extends BaseSelectMenuBuilder { static { __name(this, "StringSelectMenuBuilder"); } /** * The options within this select menu. */ options; /** * 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 StringSelectMenuBuilder({ * custom_id: 'a cool select menu', * placeholder: 'select an option', * max_values: 2, * options: [ * { label: 'option 1', value: '1' }, * { label: 'option 2', value: '2' }, * { label: 'option 3', value: '3' }, * ], * }); * ``` * @example * Creating a select menu using setters and API data: * ```ts * const selectMenu = new StringSelectMenuBuilder({ * custom_id: 'a cool select menu', * }) * .setMinValues(1) * .addOptions({ * label: 'Catchy', * value: 'catch', * }); * ``` */ constructor(data) { const { options, ...initData } = data ?? {}; super({ ...initData, type: ComponentType5.StringSelect }); this.options = options?.map((option) => new StringSelectMenuOptionBuilder(option)) ?? []; } /** * Adds options to this select menu. * * @param options - The options to add */ addOptions(...options) { const normalizedOptions = normalizeArray(options); optionsLengthValidator.parse(this.options.length + normalizedOptions.length); this.options.push( ...normalizedOptions.map( (normalizedOption) => normalizedOption instanceof StringSelectMenuOptionBuilder ? normalizedOption : new StringSelectMenuOptionBuilder(jsonOptionValidator.parse(normalizedOption)) ) ); return this; } /** * Sets the options for this select menu. * * @param options - The options to set */ setOptions(...options) { return this.spliceOptions(0, this.options.length, ...options); } /** * Removes, replaces, or inserts options for this select menu. * * @remarks * This method behaves similarly * to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice | Array.prototype.splice()}. * It's useful for modifying and adjusting the order of existing options. * @example * Remove the first option: * ```ts * selectMenu.spliceOptions(0, 1); * ``` * @example * Remove the first n option: * ```ts * const n = 4; * selectMenu.spliceOptions(0, n); * ``` * @example * Remove the last option: * ```ts * selectMenu.spliceOptions(-1, 1); * ``` * @param index - The index to start at * @param deleteCount - The number of options to remove * @param options - The replacing option objects or builders */ spliceOptions(index, deleteCount, ...options) { const normalizedOptions = normalizeArray(options); const clone = [...this.options]; clone.splice( index, deleteCount, ...normalizedOptions.map( (normalizedOption) => normalizedOption instanceof StringSelectMenuOptionBuilder ? normalizedOption : new StringSelectMenuOptionBuilder(jsonOptionValidator.parse(normalizedOption)) ) ); optionsLengthValidator.parse(clone.length); this.options.splice(0, this.options.length, ...clone); return this; } /** * {@inheritDoc BaseSelectMenuBuilder.toJSON} */ toJSON() { validateRequiredSelectMenuParameters(this.options, this.data.custom_id); return { ...this.data, options: this.options.map((option) => option.toJSON()) }; } }; // src/components/selectMenu/UserSelectMenu.ts import { ComponentType as ComponentType6, SelectMenuDefaultValueType as SelectMenuDefaultValueType4 } from "discord-api-types/v10"; var UserSelectMenuBuilder = class extends BaseSelectMenuBuilder { static { __name(this, "UserSelectMenuBuilder"); } /** * 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 UserSelectMenuBuilder({ * 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 UserSelectMenuBuilder({ * custom_id: 'a cool select menu', * }) * .setMinValues(1); * ``` */ constructor(data) { super({ ...data, type: ComponentType6.UserSelect }); } /** * Adds default users to this auto populated select menu. * * @param users - The users to add */ addDefaultUsers(...users) { 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: SelectMenuDefaultValueType4.User })) ); return this; } /** * Sets default users for this auto populated select menu. * * @param users - The users to set */ setDefaultUsers(...users) { const normalizedValues = normalizeArray(users); optionsLengthValidator.parse(normalizedValues.length); this.data.default_values = normalizedValues.map((id) => ({ id, type: SelectMenuDefaultValueType4.User })); return this; } }; // src/components/textInput/TextInput.ts import { isJSONEncodable } from "@discordjs/util"; import { ComponentType as ComponentType7 } from "discord-api-types/v10"; import isEqual from "fast-deep-equal"; // src/components/textInput/Assertions.ts var Assertions_exports3 = {}; __export(Assertions_exports3, { labelValidator: () => labelValidator, maxLengthValidator: () => maxLengthValidator, minLengthValidator: () => minLengthValidator, placeholderValidator: () => placeholderValidator2, requiredValidator: () => requiredValidator, textInputStyleValidator: () => textInputStyleValidator, validateRequiredParameters: () => validateRequiredParameters, valueValidator: () => valueValidator }); import { s as s3 } from "@sapphire/shapeshift"; import { TextInputStyle } from "discord-api-types/v10"; var textInputStyleValidator = s3.nativeEnum(TextInputStyle); var minLengthValidator = s3.number().int().greaterThanOrEqual(0).lessThanOrEqual(4e3).setValidationEnabled(isValidationEnabled); var maxLengthValidator = s3.number().int().greaterThanOrEqual(1).lessThanOrEqual(4e3).setValidationEnabled(isValidationEnabled); var requiredValidator = s3.boolean(); var valueValidator = s3.string().lengthLessThanOrEqual(4e3).setValidationEnabled(isValidationEnabled); var placeholderValidator2 = s3.string().lengthLessThanOrEqual(100).setValidationEnabled(isValidationEnabled); var labelValidator = s3.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(45).setValidationEnabled(isValidationEnabled); function validateRequiredParameters(customId, style, label) { customIdValidator.parse(customId); textInputStyleValidator.parse(style); labelValidator.parse(label); } __name(validateRequiredParameters, "validateRequiredParameters"); // src/components/textInput/TextInput.ts var TextInputBuilder = class extends ComponentBuilder { static { __name(this, "TextInputBuilder"); } /** * Creates a new text input from API data. * * @param data - The API data to create this text input with * @example * Creating a text input from an API data object: * ```ts * const textInput = new TextInputBuilder({ * custom_id: 'a cool text input', * label: 'Type something', * style: TextInputStyle.Short, * }); * ``` * @example * Creating a text input using setters and API data: * ```ts * const textInput = new TextInputBuilder({ * label: 'Type something else', * }) * .setCustomId('woah') * .setStyle(TextInputStyle.Paragraph); * ``` */ constructor(data) { super({ type: ComponentType7.TextInput, ...data }); } /** * Sets the custom id for this text input. * * @param customId - The custom id to use */ setCustomId(customId) { this.data.custom_id = customIdValidator.parse(customId); return this; } /** * Sets the label for this text input. * * @param label - The label to use */ setLabel(label) { this.data.label = labelValidator.parse(label); return this; } /** * Sets the style for this text input. * * @param style - The style to use */ setStyle(style) { this.data.style = textInputStyleValidator.parse(style); return this; } /** * Sets the minimum length of text for this text input. * * @param minLength - The minimum length of text for this text input */ setMinLength(minLength) { this.data.min_length = minLengthValidator.parse(minLength); return this; } /** * Sets the maximum length of text for this text input. * * @param maxLength - The maximum length of text for this text input */ setMaxLength(maxLength) { this.data.max_length = maxLengthValidator.parse(maxLength); return this; } /** * Sets the placeholder for this text input. * * @param placeholder - The placeholder to use */ setPlaceholder(placeholder) { this.data.placeholder = placeholderValidator2.parse(placeholder); return this; } /** * Sets the value for this text input. * * @param value - The value to use */ setValue(value) { this.data.value = valueValidator.parse(value); return this; } /** * Sets whether this text input is required. * * @param required - Whether this text input is required */ setRequired(required = true) { this.data.required = requiredValidator.parse(required); return this; } /** * {@inheritDoc ComponentBuilder.toJSON} */ toJSON() { validateRequiredParameters(this.data.custom_id, this.data.style, this.data.label); return { ...this.data }; } /** * Whether this is equal to another structure. */ equals(other) { if (isJSONEncodable(other)) { return isEqual(other.toJSON(), this.data); } return isEqual(other, this.data); } }; // src/components/v2/Container.ts import { ComponentType as ComponentType12 } from "discord-api-types/v10"; // src/components/v2/Assertions.ts var Assertions_exports4 = {}; __export(Assertions_exports4, { accessoryPredicate: () => accessoryPredicate, assertReturnOfBuilder: () => assertReturnOfBuilder, containerColorPredicate: () => containerColorPredicate, descriptionPredicate: () => descriptionPredicate2, dividerPredicate: () => dividerPredicate, filePredicate: () => filePredicate, spacingPredicate: () => spacingPredicate, spoilerPredicate: () => spoilerPredicate, textDisplayContentPredicate: () => textDisplayContentPredicate, unfurledMediaItemPredicate: () => unfurledMediaItemPredicate, validateComponentArray: () => validateComponentArray }); import { s as s4 } from "@sapphire/shapeshift"; import { SeparatorSpacingSize } from "discord-api-types/v10"; // src/components/v2/Thumbnail.ts import { ComponentType as ComponentType8 } from "discord-api-types/v10"; var ThumbnailBuilder = class extends ComponentBuilder { static { __name(this, "ThumbnailBuilder"); } /** * Creates a new thumbnail from API data. * * @param data - The API data to create this thumbnail with * @example * Creating a thumbnail from an API data object: * ```ts * const thumbnail = new ThumbnailBuilder({ * description: 'some text', * media: { * url: 'https://cdn.discordapp.com/embed/avatars/4.png', * }, * }); * ``` * @example * Creating a thumbnail using setters and API data: * ```ts * const thumbnail = new ThumbnailBuilder({ * media: { * url: 'attachment://image.png', * }, * }) * .setDescription('alt text'); * ``` */ constructor(data = {}) { super({ type: ComponentType8.Thumbnail, ...data, media: data.media ? { url: data.media.url } : void 0 }); } /** * Sets the description of this thumbnail. * * @param description - The description to use */ setDescription(description) { this.data.description = descriptionPredicate2.parse(description); return this; } /** * Clears the description of this thumbnail. */ clearDescription() { this.data.description = void 0; return this; } /** * Sets the spoiler status of this thumbnail. * * @param spoiler - The spoiler status to use */ setSpoiler(spoiler = true) { this.data.spoiler = spoilerPredicate.parse(spoiler); return this; } /** * Sets the media URL of this thumbnail. * * @param url - The URL to use */ setURL(url) { this.data.media = unfurledMediaItemPredicate.parse({ url }); return this; } /** * {@inheritdoc ComponentBuilder.toJSON} */ toJSON() { unfurledMediaItemPredicate.parse(this.data.media); return { ...this.data }; } }; // src/components/v2/Assertions.ts var unfurledMediaItemPredicate = s4.object({ url: s4.string().url( { allowedProtocols: ["http:", "https:", "attachment:"] }, { message: "Invalid protocol for media URL. Must be http:, https:, or attachment:" } ) }).setValidationEnabled(isValidationEnabled); var descriptionPredicate2 = s4.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(1024).setValidationEnabled(isValidationEnabled); var filePredicate = s4.object({ url: s4.string().url({ allowedProtocols: ["attachment:"] }, { message: "Invalid protocol for file URL. Must be attachment:" }) }).setValidationEnabled(isValidationEnabled); var spoilerPredicate = s4.boolean(); var dividerPredicate = s4.boolean(); var spacingPredicate = s4.nativeEnum(SeparatorSpacingSize); var textDisplayContentPredicate = s4.string().lengthGreaterThanOrEqual(1).lengthLessThanOrEqual(4e3).setValidationEnabled(isValidationEnabled); var accessoryPredicate = s4.instance(ButtonBuilder).or(s4.instance(ThumbnailBuilder)).setValidationEnabled(isValidationEnabled); var containerColorPredicate = colorPredicate.nullish(); function assertReturnOfBuilder(input, ExpectedInstanceOf) { s4.instance(ExpectedInstanceOf).setValidationEnabled(isValidationEnabled).parse(input); } __name(assertReturnOfBuilder, "assertReturnOfBuilder"); function validateComponentArray(input, min, max, ExpectedInstanceOf) { (ExpectedInstanceOf ? s4.instance(ExpectedInstanceOf) : s4.instance(ComponentBuilder)).array().lengthGreaterThanOrEqual(min).lengthLessThanOrEqual(max).setValidationEnabled(isValidationEnabled).parse(input); } __name(validateComponentArray, "validateComponentArray"); // src/components/v2/File.ts import { ComponentType as ComponentType9 } from "discord-api-types/v10"; var FileBuilder = class extends ComponentBuilder { static { __name(this, "FileBuilder"); } /** * Creates a new file from API data. * * @param data - The API data to create this file with * @example * Creating a file from an API data object: * ```ts * const file = new FileBuilder({ * spoiler: true, * file: { * url: 'attachment://file.png', * }, * }); * ``` * @example * Creating a file using setters and API data: * ```ts * const file = new FileBuilder({ * file: { * url: 'attachment://image.jpg', * }, * }) * .setSpoiler(false); * ``` */ constructor(data = {}) { super({ type: ComponentType9.File, ...data, file: data.file ? { url: data.file.url } : void 0 }); } /** * Sets the spoiler status of this file. * * @param spoiler - The spoiler status to use */ setSpoiler(spoiler = true) { this.data.spoiler = spoilerPredicate.parse(spoiler); return this; } /** * Sets the media URL of this file. * * @param url - The URL to use */ setURL(url) { this.data.file = filePredicate.parse({ url }); return this; } /** * {@inheritDoc ComponentBuilder.toJSON} */ toJSON() { filePredicate.parse(this.data.file); return { ...this.data, file: { ...this.data.file } }; } }; // src/components/v2/Separator.ts import { ComponentType as ComponentType10 } from "discord-api-types/v10"; var SeparatorBuilder = class extends ComponentBuilder { static { __name(this, "SeparatorBuilder"); } /** * Creates a new separator from API data. * * @param data - The API data to create this separator with * @example * Creating a separator from an API data object: * ```ts * const separator = new SeparatorBuilder({ * spacing: SeparatorSpacingSize.Small, * divider: true, * }); * ``` * @example * Creating a separator using setters and API data: * ```ts * const separator = new SeparatorBuilder({ * spacing: SeparatorSpacingSize.Large, * }) * .setDivider(false); * ``` */ constructor(data = {}) { super({ type: ComponentType10.Separator, ...data }); } /** * Sets whether this separator should show a divider line. * * @param divider - Whether to show a divider line */ setDivider(divider = true) { this.data.divider = dividerPredicate.parse(divider); return this; } /** * Sets the spacing of this separator. * * @param spacing - The spacing to use */ setSpacing(spacing) { this.data.spacing = spacingPredicate.parse(spacing); return this; } /** * Clears the spacing of this separator. */ clearSpacing() { this.data.spacing = void 0; return this; } /** * {@inheritDoc ComponentBuilder.toJSON} */ toJSON() { return { ...this.data }; } }; // src/components/v2/TextDisplay.ts import { ComponentType as ComponentType11 } from "discord-api-types/v10"; var TextDisplayBuilder = class extends ComponentBuilder { static { __name(this, "TextDisplayBuilder"); } /** * Creates a new text display from API data. * * @param data - The API data to create this text display with * @example * Creating a text display from an API data object: * ```ts * const textDisplay = new TextDisplayBuilder({ * content: 'some text', * }); * ``` * @example * Creating a text display using setters and API data: * ```ts * const textDisplay = new TextDisplayBuilder({ * content: 'old text', * }) * .setContent('new text'); * ``` */ constructor(data = {}) { super({ type: ComponentType11.TextDisplay, ...data }); } /** * Sets the text of this text display. * * @param content - The text to use */ setContent(content) { this.data.content = textDisplayContentPredicate.parse(content); return this; } /** * {@inheritDoc ComponentBuilder.toJSON} */ toJSON() { textDisplayContentPredicate.parse(this.data.content); return { ...this.data }; } }; // src/components/v2/Container.ts var ContainerBuilder = class extends ComponentBuilder { static { __name(this, "ContainerBuilder"); } /** * The components within this container. */ components; /** * Creates a new container from API data. * * @param data - The API data to create this container with * @example * Creating a container from an API data object: * ```ts * const container = new ContainerBuilder({ * components: [ * { * content: "Some text here",