Skip to content

Commit

Permalink
feat: allow both rest and array parameter
Browse files Browse the repository at this point in the history
Breaking Change: Removed singular methods.
Breaking Change: ExtraRowPosition is an enum now.

More infos in discordjs/discord.js#7522
and discordjs/discord.js#7874
  • Loading branch information
imranbarbhuiya committed Jun 24, 2022
1 parent 10c9f43 commit 9c4d7a2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 68 deletions.
93 changes: 25 additions & 68 deletions src/lib/pagination/PaginationEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
EmbedBuilder,
MessageActionRowComponentBuilder,
ComponentEmojiResolvable,
JSONEncodable
JSONEncodable,
RestOrArray,
normalizeArray
} from 'discord.js';
import type { PAttachments, ButtonsOptions, ButtonStyle, PEmbeds, EmojiOptions, LabelOptions, Options, Payload } from '../types';
import { PAttachments, ButtonsOptions, ButtonStyle, PEmbeds, EmojiOptions, LabelOptions, Options, Payload, ExtraRowPosition } from '../types';
import { defaultOptions } from './defaultOptions';

/**
Expand Down Expand Up @@ -172,7 +174,7 @@ export abstract class PaginationEmbed extends EmbedBuilder {
*/
private extraRows: {
rows: ActionRowBuilder<MessageActionRowComponentBuilder>[];
position: 'above' | 'below';
position: ExtraRowPosition;
}[];

/**
Expand Down Expand Up @@ -248,13 +250,19 @@ export abstract class PaginationEmbed extends EmbedBuilder {
this.setOptions(mergedOptions);
}

public override setFields(fields: APIEmbedField[]): this {
this.rawFields = fields;
public override addFields(...fields: RestOrArray<APIEmbedField>): this {
this.rawFields.push(...normalizeArray(fields));
return this;
}

public override addFields(fields: APIEmbedField[]): this {
this.rawFields.push(...fields);
public spliceFields(index: number, deleteCount: number, ...fields: APIEmbedField[]): this {
if (this.data.fields) this.data.fields.splice(index, deleteCount, ...fields);
else this.data.fields = fields;
return this;
}

public override setFields(...fields: RestOrArray<APIEmbedField>): this {
this.rawFields = normalizeArray(fields);
return this;
}

Expand Down Expand Up @@ -319,25 +327,8 @@ export abstract class PaginationEmbed extends EmbedBuilder {
* ```
*
*/
public setImages(images: string[]): this {
this.images = images;
return this;
}

/**
* Adds a pagination image.
* @param image
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction)
* .setImages(["1st image", "2nd image", "3rd image"])
* .addImage("4st image");
* ```
*
*/
public addImage(image: string): this {
this.images.push(image);
public setImages(...images: RestOrArray<string>): this {
this.images = normalizeArray(images);
return this;
}

Expand All @@ -353,8 +344,8 @@ export abstract class PaginationEmbed extends EmbedBuilder {
* ```
*
*/
public addImages(images: string[]): this {
this.images.push(...images);
public addImages(...images: RestOrArray<string>): this {
this.images.push(...normalizeArray(images));
return this;
}

Expand All @@ -369,25 +360,8 @@ export abstract class PaginationEmbed extends EmbedBuilder {
* ```
*
*/
public setDescriptions(descriptions: string[]): this {
this.descriptions = descriptions;
return this;
}

/**
* Adds a pagination description.
* @param description
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction)
* .setDescriptions(["1st description", "2nd description", "3rd description"])
* .addDescription("4st description");
* ```
*
*/
public addDescription(description: string): this {
this.descriptions.push(description);
public setDescriptions(...descriptions: RestOrArray<string>): this {
this.descriptions = normalizeArray(descriptions);
return this;
}

Expand All @@ -403,8 +377,8 @@ export abstract class PaginationEmbed extends EmbedBuilder {
* ```
*
*/
public addDescriptions(descriptions: string[]): this {
this.descriptions.push(...descriptions);
public addDescriptions(...descriptions: RestOrArray<string>): this {
this.descriptions.push(...normalizeArray(descriptions));
return this;
}

Expand All @@ -431,23 +405,6 @@ export abstract class PaginationEmbed extends EmbedBuilder {
return this;
}

/**
* Adds a pagination embed.
* @param embed
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction)
* .setEmbeds([new EmbedBuilder(), new EmbedBuilder(), new EmbedBuilder()])
* .addEmbed(new EmbedBuilder);
* ```
*
*/
public addEmbed(embed: PEmbeds[number]): this {
this.embeds.push(embed);
return this;
}

/**
* Adds multiple pagination embeds.
* @param embeds An array of {@link EmbedBuilder} or {@link EmbedBuilderOptions}
Expand Down Expand Up @@ -710,7 +667,7 @@ export abstract class PaginationEmbed extends EmbedBuilder {
* ```
*
*/
public addActionRows(actionRows: ActionRowBuilder<MessageActionRowComponentBuilder>[], position: 'below' | 'above' = 'below'): this {
public addActionRows(actionRows: ActionRowBuilder<MessageActionRowComponentBuilder>[], position = ExtraRowPosition.Below): this {
this.extraRows.push({
rows: actionRows,
position
Expand Down Expand Up @@ -992,7 +949,7 @@ export abstract class PaginationEmbed extends EmbedBuilder {
this.actionRows = [this.mainActionRow];
if (this.extraRows.length > 0) {
this.extraRows.forEach((row) => {
row.position === 'above' ? this.actionRows.unshift(...row.rows) : this.actionRows.push(...row.rows);
row.position === ExtraRowPosition.Above ? this.actionRows.unshift(...row.rows) : this.actionRows.push(...row.rows);
});
}
return this;
Expand Down
5 changes: 5 additions & 0 deletions src/lib/types/Options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import type { EmojiOptions } from './EmojiOptions';
export type PEmbeds = Required<MessageOptions>['embeds'];
export type PAttachments = Required<MessageOptions>['files'];

export enum ExtraRowPosition {
Above,
Below
}

/**
* The options to customize the pagination.
*/
Expand Down

0 comments on commit 9c4d7a2

Please sign in to comment.