From 4e35a7fdb622dfd1286b629fb49a6a64d3e62256 Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Sun, 6 Feb 2022 18:51:14 -0700 Subject: [PATCH] account for possibility of no desc in pagination --- util/livepagination.d.ts | 18 +++++++++++++++ util/livepagination.js | 47 ++++++++++++++++++++++++++++++++++++++++ util/pagination.js | 2 +- util/ts/pagination.ts | 2 +- 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 util/livepagination.d.ts create mode 100644 util/livepagination.js diff --git a/util/livepagination.d.ts b/util/livepagination.d.ts new file mode 100644 index 0000000..a1fbb1a --- /dev/null +++ b/util/livepagination.d.ts @@ -0,0 +1,18 @@ +import { TextChannel, Message, MessageEmbed, Client } from 'discord.js'; +import { Pagination } from './pagination'; +export declare class LivePagination extends Pagination { + knownMax: number; + private _onScrollAttempt; + constructor(channel: TextChannel, pages: MessageEmbed[], originalMessage: Message, client: Client, loopPages?: boolean, message?: Message); + setOnScrollAttemptHandler(func: (pagination: LivePagination, pos: number, exists: boolean, inBounds: boolean) => void): this; + start(options?: { + endTime?: number; + time?: number; + startPage?: number; + user?: 'any' | string; + }): Promise; + setControllers(endTime: number, user: 'any' | string): Promise; + setPage(page: number): Promise; + private static throwNoScrollAttempt; + set onScrollAttempt(func: (pagination: LivePagination, pos: number, exists: boolean, inBounds: boolean) => void); +} diff --git a/util/livepagination.js b/util/livepagination.js new file mode 100644 index 0000000..1abd41b --- /dev/null +++ b/util/livepagination.js @@ -0,0 +1,47 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.LivePagination = void 0; +const pagination_1 = require("./pagination"); +class LivePagination extends pagination_1.Pagination { + constructor(channel, pages, originalMessage, client, loopPages, message) { + super(channel, pages, originalMessage, client, loopPages, message); + } + ; + setOnScrollAttemptHandler(func) { + this._onScrollAttempt = func; + return this; + } + ; + async start(options) { + if (!this._onScrollAttempt) { + LivePagination.throwNoScrollAttempt(); + } + await super.start(options); + return this; + } + async setControllers(endTime, user) { + if (!this._onScrollAttempt) { + LivePagination.throwNoScrollAttempt(); + } + await super.setControllers(endTime, user); + return this; + } + ; + async setPage(page) { + if (!this._onScrollAttempt) { + LivePagination.throwNoScrollAttempt(); + } + this._onScrollAttempt(this, page, typeof this.pages[page] !== 'undefined' && this.pages[page] !== null, typeof this.knownMax === 'number' ? page < this.knownMax : true); + await super.setPage(page); + return this; + } + ; + static throwNoScrollAttempt() { + throw new Error("Fatal Pagination Error: You tried to start the LivePagination without setting a scrollAttemptEvent. This is necessary to allow the pagination to be built as the user scrolls. If you don't know what you're doing here, just make a Pagination instead."); + } + set onScrollAttempt(func) { + this._onScrollAttempt = func; + } + ; +} +exports.LivePagination = LivePagination; diff --git a/util/pagination.js b/util/pagination.js index 9afb725..11ae061 100644 --- a/util/pagination.js +++ b/util/pagination.js @@ -117,7 +117,7 @@ class Pagination { } this.controllers.collector.stop(); let fe = this.getCurrentPage(); - fe.setDescription(`${fe.description}\n\n*This menu has ended, start a new one to interact with it!*`); + fe.setDescription(`${fe.description && fe.description.length ? `${fe.description}\n\n` : ''}*This menu has ended, start a new one to interact with it!*`); fe.setFooter({ text: `Menu Ended${fe.footer && fe.footer.text && fe.footer.text.length ? ` | ${fe.footer.text}` : ''}`, iconURL: this.client.user.displayAvatarURL() }); await this.message.edit({ embeds: [fe] }); clearInterval(this.timeoutInterval); diff --git a/util/ts/pagination.ts b/util/ts/pagination.ts index efbeb5a..85ed4fd 100644 --- a/util/ts/pagination.ts +++ b/util/ts/pagination.ts @@ -125,7 +125,7 @@ export class Pagination { this.controllers.collector.stop(); let fe = this.getCurrentPage(); - fe.setDescription(`${fe.description}\n\n*This menu has ended, start a new one to interact with it!*`); + fe.setDescription(`${fe.description && fe.description.length ? `${fe.description}\n\n` : ''}*This menu has ended, start a new one to interact with it!*`); fe.setFooter({text: `Menu Ended${fe.footer && fe.footer.text && fe.footer.text.length ? ` | ${fe.footer.text}` : ''}`, iconURL: this.client.user.displayAvatarURL()}); await this.message.edit({embeds: [fe]});