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 23, 2022
1 parent 92cfe83 commit 36647cb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 75 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"@typescript-eslint/eslint-plugin": "^5.28.0",
"@typescript-eslint/parser": "^5.28.0",
"cz-conventional-changelog": "^3.3.0",
"discord.js": "^14.0.0-dev.1654519696-fba9710",
"discord.js": "^14.0.0-dev.1654560560-5475767",
"eslint": "^8.17.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.0.0",
Expand Down
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
19 changes: 13 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1663,22 +1663,29 @@ __metadata:
languageName: node
linkType: hard

"discord.js@npm:^14.0.0-dev.1654519696-fba9710":
version: 14.0.0-dev.1654519696-fba9710
resolution: "discord.js@npm:14.0.0-dev.1654519696-fba9710"
"discord-api-types@npm:^0.33.4":
version: 0.33.4
resolution: "discord-api-types@npm:0.33.4"
checksum: 900fe88caedd215d3cc89a3ac39da4ee5d0d80e08149790bbaaea48d0c9029d72ff6936870ac5055af4f5de04ebffeba6f80fa2c0a00d37d784bd72bb914114e
languageName: node
linkType: hard

"discord.js@npm:^14.0.0-dev.1654560560-5475767":
version: 14.0.0-dev.1654560560-5475767
resolution: "discord.js@npm:14.0.0-dev.1654560560-5475767"
dependencies:
"@discordjs/builders": ^0.16.0-dev
"@discordjs/collection": ^0.8.0-dev
"@discordjs/rest": ^0.6.0-dev
"@sapphire/snowflake": ^3.2.2
"@types/ws": ^8.5.3
discord-api-types: ^0.33.3
discord-api-types: ^0.33.4
fast-deep-equal: ^3.1.3
lodash.snakecase: ^4.1.1
tslib: ^2.4.0
undici: ^5.4.0
ws: ^8.7.0
checksum: d2721d59bf2782f44e968f2479bbcae6ced43079623dd8394577c3fd5674ccb2badd6f7e3f2df4e6fbbee667feb920efe270d304929ac25d6d907d9be66f0e90
checksum: 954b541a9d9445a8e1b0085693839fdfb1a13b2637ff74dff23058ce0b1a91d461fd4edeac5ce640ff4ceabcb7d95d63c79b9672dde5abbcda7efc0cffa45ee0
languageName: node
linkType: hard

Expand Down Expand Up @@ -3977,7 +3984,7 @@ __metadata:
"@typescript-eslint/eslint-plugin": ^5.28.0
"@typescript-eslint/parser": ^5.28.0
cz-conventional-changelog: ^3.3.0
discord.js: ^14.0.0-dev.1654519696-fba9710
discord.js: ^14.0.0-dev.1654560560-5475767
eslint: ^8.17.0
eslint-config-prettier: ^8.5.0
eslint-plugin-prettier: ^4.0.0
Expand Down

0 comments on commit 36647cb

Please sign in to comment.