seyfert
Version:
The most advanced framework for discord bots
111 lines (110 loc) • 3.35 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaGalleryItem = exports.MediaGallery = void 0;
const types_1 = require("../types");
const Base_1 = require("./Base");
/**
* Represents a media gallery component builder.
* Used to display a collection of media items.
* @example
* ```ts
* const gallery = new MediaGallery()
* .addItems(
* new MediaGalleryItem().setMedia('https://example.com/image1.png').setDescription('Image 1'),
* new MediaGalleryItem().setMedia('https://example.com/image2.jpg').setSpoiler()
* );
* ```
*/
class MediaGallery extends Base_1.BaseComponentBuilder {
items;
/**
* Constructs a new MediaGallery.
* @param data Optional initial data for the media gallery.
*/
constructor({ items, ...data } = {}) {
super({ type: types_1.ComponentType.MediaGallery, ...data });
this.items = (items?.map(i => new MediaGalleryItem(i)) ?? []);
}
/**
* Sets the ID for the media gallery component.
* @param id The ID to set.
* @returns The updated MediaGallery instance.
*/
setId(id) {
this.data.id = id;
return this;
}
/**
* Adds items to the media gallery.
* @param items The items to add. Can be a single item, an array of items, or multiple items as arguments.
* @returns The updated MediaGallery instance.
*/
addItems(...items) {
this.items = this.items.concat(items.flat());
return this;
}
/**
* Sets the items for the media gallery, replacing any existing items.
* @param items The items to set. Can be a single item, an array of items, or multiple items as arguments.
* @returns The updated MediaGallery instance.
*/
setItems(...items) {
this.items = items.flat();
return this;
}
toJSON() {
return {
...this.data,
items: this.items.map(i => i.toJSON()),
};
}
}
exports.MediaGallery = MediaGallery;
/**
* Represents an item within a MediaGallery.
*/
class MediaGalleryItem {
data;
/**
* Constructs a new MediaGalleryItem.
* @param data Optional initial data for the media gallery item.
*/
constructor(data = {}) {
this.data = data;
}
/**
* Sets the media URL for this gallery item.
* @param url The URL of the media.
* @returns The updated MediaGalleryItem instance.
*/
setMedia(url) {
this.data.media = { url };
return this;
}
/**
* Sets the description for this gallery item.
* @param desc The description text.
* @returns The updated MediaGalleryItem instance.
*/
setDescription(desc) {
this.data.description = desc;
return this;
}
/**
* Sets whether this gallery item should be visually marked as a spoiler.
* @param spoiler Whether the item is a spoiler (defaults to true).
* @returns The updated MediaGalleryItem instance.
*/
setSpoiler(spoiler = true) {
this.data.spoiler = spoiler;
return this;
}
/**
* Converts this MediaGalleryItem instance to its JSON representation.
* @returns The JSON representation of the item data.
*/
toJSON() {
return { ...this.data };
}
}
exports.MediaGalleryItem = MediaGalleryItem;