seyfert/src/builders/MediaGallery.ts
Marcos Susaña e3b6f57741
chore: 3.1.0 (#339)
* perf: optimize members cache

* feat: components V2 (#337)

* feat: components v2

* fix: build

* chore: apply formatting

* refactor(components): some types

* refactor(types): replace TopLevelComponents with APITopLevelComponent in REST

* fix: unify components

* refactor(TextDisplay): rename content method to setContent for clarity

* refactor(builders): add missing builder from component

* fix: touche

* feat(webhook): webhook params for components v2

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* fix: use protected instead of private

* fix(editOrReply): accept flags when editing message

* feat: add onBeforeMiddlewares and onBeforeOptions (#338)

* chore: package version

---------

Co-authored-by: MARCROCK22 <marcos22dev@gmail.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: MARCROCK22 <57925328+MARCROCK22@users.noreply.github.com>
2025-04-27 01:21:56 -04:00

113 lines
3.2 KiB
TypeScript

import type { RestOrArray } from '../common';
import { type APIMediaGalleryComponent, type APIMediaGalleryItems, ComponentType } from '../types';
import { BaseComponentBuilder } from './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()
* );
* ```
*/
export class MediaGallery extends BaseComponentBuilder<APIMediaGalleryComponent> {
items: MediaGalleryItem[];
/**
* Constructs a new MediaGallery.
* @param data Optional initial data for the media gallery.
*/
constructor({ items, ...data }: Partial<APIMediaGalleryComponent> = {}) {
super({ type: ComponentType.MediaGallery, ...data });
this.items = (items?.map(i => new MediaGalleryItem(i)) ?? []) as MediaGalleryItem[];
}
/**
* Sets the ID for the media gallery component.
* @param id The ID to set.
* @returns The updated MediaGallery instance.
*/
setId(id: number) {
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: RestOrArray<MediaGalleryItem>) {
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: RestOrArray<MediaGalleryItem>) {
this.items = items.flat();
return this;
}
toJSON() {
return {
...this.data,
items: this.items.map(i => i.toJSON()),
} as APIMediaGalleryComponent;
}
}
/**
* Represents an item within a MediaGallery.
*/
export class MediaGalleryItem {
/**
* Constructs a new MediaGalleryItem.
* @param data Optional initial data for the media gallery item.
*/
constructor(public data: Partial<APIMediaGalleryItems> = {}) {}
/**
* Sets the media URL for this gallery item.
* @param url The URL of the media.
* @returns The updated MediaGalleryItem instance.
*/
setMedia(url: string) {
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: string) {
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 };
}
}