seyfert
Version:
The most advanced framework for discord bots
128 lines (127 loc) • 4.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckboxGroupOption = exports.CheckboxGroup = void 0;
const common_1 = require("../common");
const types_1 = require("../types");
const Base_1 = require("./Base");
class CheckboxGroup extends Base_1.BaseComponentBuilder {
#options = [];
constructor(data = {}) {
super({ type: types_1.ComponentType.CheckboxGroup, ...data });
}
/**
* Sets the ID for the checkbox group.
* @param id - The ID for the checkbox group.
* @return The current CheckboxGroup instance.
* @remarks The ID is used by Discord to identify the component when an interaction is received. It must be unique within the message.
*/
setId(id) {
this.data.id = id;
return this;
}
/**
* Sets the custom ID for the checkbox group.
* @param customId - The custom ID for the checkbox group.
* @returns The current CheckboxGroup instance.
*/
setCustomId(customId) {
this.data.custom_id = customId;
return this;
}
/**
* Sets the maximum and minimum number of selected values for the checkbox group.
* [max = options length] 10, [min = 1] 0
* @param options - The maximum and minimum values.
* @returns The current CheckboxGroup instance.
*/
setSelectionLimit({ max, min }) {
this.data.max_values = max;
this.data.min_values = min;
return this;
}
/**
* Sets whether the checkbox group is required.
* @param required - Whether the checkbox group is required (true by discord side).
* @returns The current CheckboxGroup instance.
*/
setRequired(required) {
this.data.required = required;
return this;
}
setOptions(...options) {
this.#options = options.flat();
return this;
}
addOptions(...options) {
this.#options = this.#options.concat(options.flat());
return this;
}
toJSON() {
const options = [...this.#options.map(option => option.toJSON()), ...(this.data.options ?? [])];
const optionCount = options.length;
if (optionCount < 2 || optionCount > 10) {
throw new common_1.SeyfertError('INVALID_OPTIONS_LENGTH', {
metadata: {
...(0, common_1.createValidationMetadata)('number of options between 2 and 10', optionCount, {
component: 'CheckboxGroup',
}),
detail: 'CheckboxGroup must have between 2 and 10 options.',
},
});
}
return {
...this.data,
options,
};
}
}
exports.CheckboxGroup = CheckboxGroup;
class CheckboxGroupOption {
data;
constructor(data) {
this.data = data;
}
/**
* Sets the value of the checkbox group option.
* @param value - The value of the checkbox group option.
* @return The current CheckboxGroupOption instance.
* @remarks The value is sent to the bot when the option is selected. It must be unique within the options of the checkbox group.
* The value can be up to 100 characters long.
*/
setValue(value) {
this.data.value = value;
return this;
}
/**
* Sets the label of the checkbox group option.
* @param label - The label of the checkbox group option.
* @return The current CheckboxGroupOption instance.
* @remarks The label is displayed to the user. It can be up to 100 characters long.
*/
setLabel(label) {
this.data.label = label;
return this;
}
/**
* Sets the description of the checkbox group option.
* @param description - The description of the checkbox group option.
* @return The current CheckboxGroupOption instance.
*/
setDescription(description) {
this.data.description = description;
return this;
}
/**
* Sets whether the checkbox group option is default.
* @param value - Whether the checkbox group option is default.
* @return The current CheckboxGroupOption instance.
*/
setDefault(value) {
this.data.default = value;
return this;
}
toJSON() {
return { ...this.data };
}
}
exports.CheckboxGroupOption = CheckboxGroupOption;