UNPKG

@djs-button-pages/presets

Version:

A set of presets that are used for faster development.

158 lines (157 loc) 5.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const djs_button_pages_1 = require("djs-button-pages"); /** * Bind for button that switches pagination to the page specified by user. */ class PageTravelButton extends djs_button_pages_1.ButtonWrapper { _duration = 15000; _endPhrase = `end`; _endTip = `Type \`${this._endPhrase}\` to stop travelling.`; _startTip = `Now you have ${(this._duration / 1000).toFixed(0)} seconds, send numbers in chat to change pages!\n\nType \`${this._endPhrase}\` to exit from page travelling.`; _travelSet = new Set(); /** * Bind for button that switches pagination to the page specified by user. * @param {ButtonData} style Styling for button. */ constructor(style) { super(style); this.setAction(this._doAction); this.setSwitch(PageTravelButton._switch); } ; /** * @returns {string} Phrase that stops page travel. */ get endPhrase() { return this._endPhrase; } ; /** * @returns {string | LocaleGenerator | undefined} Tip that will appear when person presses button but he's already travelling through pages. */ get endTip() { return this._endTip; } ; /** * @returns {string | LocaleGenerator | undefined} Tip that will appear when person activates page travelling. */ get startTip() { return this._startTip; } ; /** * @returns {number} Page travel duration. */ get duration() { return this._duration; } ; /** * Sets duration for page travel. * @param {number} duration Page travel duration. * @param {boolean} bypassLimits Should this method bypass limits of library or not. * @returns {this} */ setDuration(duration, bypassLimits = false) { if (duration < 0 || !Number.isInteger(duration)) throw new Error("[@DJS-Button-Pages/Presets]: Time should be integer!"); if (!bypassLimits && duration < djs_button_pages_1.Constants.LibraryMinPageLifeTime) throw new Error("[@DJS-Button-Pages/Presets]: Page travel should last at least one second!"); if (!bypassLimits && duration > djs_button_pages_1.Constants.LibraryMaxPageLifeTime) throw new RangeError("[@DJS-Button-Pages/Presets]: Page travel time should be no more than an hour due to optimization. If you want to bypass the limit, pass true as the second argument to this method."); this._duration = duration; return this; } ; /** * Sets phrase that ends page travel. * @param {string} phrase Phrase that ends page travel. * @returns {this} */ setEndPhrase(phrase) { this._endPhrase = phrase; return this; } ; /** * Sets tip that will appear when person presses button but he's already travelling through pages. * @param {string | LocaleGenerator} tip Tip that will appear when person presses button but he's already travelling through pages. * @returns {this} */ setEndTip(tip) { this._endTip = tip; return this; } ; /** * Sets tip that will appear when person activates page travelling. * @param {string | LocaleGenerator} tip Tip that will appear when person activates page travelling. * @returns {this} */ setStartTip(tip) { this._startTip = tip; return this; } ; /** * Action that switches pagination to the page specified by user. * @param {PaginationSent} pagination Pagination. * @returns {Promise<void>} */ async _doAction(pagination, interaction) { if (this._travelSet.has(`${interaction.message.id}-${interaction.user.id}`)) { if (this._endTip) await interaction.followUp({ ephemeral: true, content: typeof this._endTip === "string" ? this._endTip : this._endTip(interaction, pagination, this) }); return; } ; const collector = interaction.channel.createMessageCollector({ filter: (msg) => msg.author.id === interaction.user.id, time: this._duration, }); if (this._startTip) await interaction.followUp({ ephemeral: true, content: typeof this._startTip === "string" ? this._startTip : this._startTip(interaction, pagination, this) }); this._travelSet.add(`${interaction.message.id}-${interaction.user.id}`); collector.on("collect", async (message) => this._collect(message, pagination, collector)); collector.on("end", () => { this._travelSet.delete(`${interaction.message.id}-${interaction.user.id}`); }); return; } ; /** * Handles collect event for message collector. * @param {Message} message Message that user wrote. * @param {PaginationSent} pagination Pagination that should be updated. * @param {MessageCollector} collector Message collector. * @returns {Promise<void>} */ async _collect(message, pagination, collector) { const content = message.content.trim().toLowerCase(); if (content === this._endPhrase) { if (message.deletable) await message.delete().catch(() => { }); return collector.stop(); } ; const int = parseInt(content); if (isNaN(int) || int > pagination.data.embeds.length || int < 1) return; if (message.deletable) await message.delete().catch(() => { }); return pagination.setPage(int - 1).update(); } ; /** * Switch that never disables the button. * @returns {false} */ static _switch() { return false; } ; } exports.default = PageTravelButton; ;