Skip to content

Commit

Permalink
fix: buttons might be optional too
Browse files Browse the repository at this point in the history
fixes #231
  • Loading branch information
imranbarbhuiya committed Jan 6, 2024
1 parent 0f86298 commit 3b30874
Show file tree
Hide file tree
Showing 7 changed files with 714 additions and 1,767 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"editor.detectIndentation": false,
"files.eol": "\n",
"editor.codeActionsOnSave": {
"source.organizeImports": false,
"source.fixAll": true
"source.organizeImports": "never",
"source.fixAll": "explicit"
}
}
541 changes: 0 additions & 541 deletions .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

This file was deleted.

9 changes: 0 additions & 9 deletions .yarn/plugins/@yarnpkg/plugin-typescript.cjs

This file was deleted.

6 changes: 0 additions & 6 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@ enableGlobalCache: true

nodeLinker: node-modules

plugins:
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
spec: '@yarnpkg/plugin-typescript'
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
spec: '@yarnpkg/plugin-interactive-tools'

yarnPath: .yarn/releases/yarn-4.0.2.cjs
21 changes: 13 additions & 8 deletions src/lib/pagination/Pagination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,20 +157,25 @@ export class Pagination extends PaginationEmbed {
componentType: ComponentType.Button
});

this.collector.on('collect', (interaction) => {
if (interaction.customId === this.buttons.first.data.custom_id) {
return this.goFirst(interaction);
this.collector.on('collect', async (interaction) => {
if (interaction.customId === this.buttons.first?.data.custom_id) {
await this.goFirst(interaction);
return;
}

if (interaction.customId === this.buttons.prev.data.custom_id) {
return this.goPrev(interaction);
if (interaction.customId === this.buttons.prev?.data.custom_id) {
await this.goPrev(interaction);
return;
}

if (interaction.customId === this.buttons.next.data.custom_id) {
return this.goNext(interaction);
if (interaction.customId === this.buttons.next?.data.custom_id) {
await this.goNext(interaction);
return;
}

return this.goLast(interaction);
if (interaction.customId === this.buttons.last?.data.custom_id) {
await this.goLast(interaction);
}
});
return this;
}
Expand Down
60 changes: 33 additions & 27 deletions src/lib/pagination/PaginationEmbed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export abstract class PaginationEmbed extends EmbedBuilder {
/**
* The pagination buttons.
*/
public buttons!: Record<string, PButtonBuilder>;
public buttons!: Record<string, PButtonBuilder | undefined>;

/**
* Contents if changing contents per page.
Expand Down Expand Up @@ -903,12 +903,12 @@ export abstract class PaginationEmbed extends EmbedBuilder {
protected async goFirst(interaction: ButtonInteraction) {
this.currentPage = 1;
if (!this.loop) {
this.buttons.first.setDisabled();
this.buttons.prev.setDisabled();
this.buttons.first?.setDisabled();
this.buttons.prev?.setDisabled();
}

this.buttons.next.setDisabled(false);
this.buttons.last.setDisabled(false);
this.buttons.next?.setDisabled(false);
this.buttons.last?.setDisabled(false);

this.goToPage(1);

Expand All @@ -930,12 +930,12 @@ export abstract class PaginationEmbed extends EmbedBuilder {
protected async goPrev(interaction: ButtonInteraction) {
this.currentPage--;
if (!this.loop) {
this.buttons.first.setDisabled(this.currentPage === 1);
this.buttons.prev.setDisabled(this.currentPage === 1);
this.buttons.first?.setDisabled(this.currentPage === 1);
this.buttons.prev?.setDisabled(this.currentPage === 1);
}

this.buttons.next.setDisabled(false);
this.buttons.last.setDisabled(false);
this.buttons.next?.setDisabled(false);
this.buttons.last?.setDisabled(false);
this.goToPage(this.currentPage);
await interaction.update(this.payload);
}
Expand All @@ -954,11 +954,11 @@ export abstract class PaginationEmbed extends EmbedBuilder {
*/
protected async goNext(interaction: ButtonInteraction) {
this.currentPage++;
this.buttons.prev.setDisabled(false);
this.buttons.first.setDisabled(false);
this.buttons.prev?.setDisabled(false);
this.buttons.first?.setDisabled(false);
if (!this.loop) {
this.buttons.next.setDisabled(this.currentPage === Math.ceil(this.totalEntry / this.limit));
this.buttons.last.setDisabled(this.currentPage === Math.ceil(this.totalEntry / this.limit));
this.buttons.next?.setDisabled(this.currentPage === Math.ceil(this.totalEntry / this.limit));
this.buttons.last?.setDisabled(this.currentPage === Math.ceil(this.totalEntry / this.limit));
}

this.goToPage(this.currentPage);
Expand All @@ -979,18 +979,24 @@ export abstract class PaginationEmbed extends EmbedBuilder {
*/
protected async goLast(interaction: ButtonInteraction) {
this.currentPage = Math.ceil(this.totalEntry / this.limit);
this.buttons.prev.setDisabled(false);
this.buttons.first.setDisabled(false);
this.buttons.prev?.setDisabled(false);
this.buttons.first?.setDisabled(false);
if (!this.loop) {
this.buttons.next.setDisabled();
this.buttons.last.setDisabled();
this.buttons.next?.setDisabled();
this.buttons.last?.setDisabled();
}

this.goToPage(this.currentPage);
await interaction.update(this.payload);
}

private _readyButton(button: ButtonBuilder, label: string | undefined, emoji: ComponentEmojiResolvable | undefined, style: PButtonStyle): this {
private _readyButton(
button: ButtonBuilder | undefined,
label: string | undefined,
emoji: ComponentEmojiResolvable | undefined,
style: PButtonStyle
): this {
if (!button) return this;
if (label) button.setLabel(label);
if (emoji) button.setEmoji(emoji);
button.setStyle(style);
Expand All @@ -1010,21 +1016,21 @@ export abstract class PaginationEmbed extends EmbedBuilder {
this._readyButton(this.buttons.last, this.buttonInfo.last.label, this.buttonInfo.last.emoji, this.buttonInfo.last.style);
}

this.buttons.first.setDisabled();
this.buttons.prev.setDisabled();
this.buttons.next.setDisabled();
this.buttons.last.setDisabled();
this.buttons.first?.setDisabled();
this.buttons.prev?.setDisabled();
this.buttons.next?.setDisabled();
this.buttons.last?.setDisabled();
if (this.totalEntry > this.limit) {
this.buttons.last.setDisabled(false);
this.buttons.next.setDisabled(false);
this.buttons.last?.setDisabled(false);
this.buttons.next?.setDisabled(false);
}

if (this.loop && this.totalEntry > this.limit) {
this.buttons.first.setDisabled(false);
this.buttons.prev.setDisabled(false);
this.buttons.first?.setDisabled(false);
this.buttons.prev?.setDisabled(false);
}

this.mainActionRow.setComponents(Object.values(this.buttons));
this.mainActionRow.setComponents(Object.values(this.buttons) as PButtonBuilder[]);
this.actionRows = [this.mainActionRow];
if (this.extraRows.length > 0) {
for (const row of this.extraRows) {
Expand Down
Loading

0 comments on commit 3b30874

Please # to comment.