How to Read Count of Emoji Collectory Discord.js

NPM info

discord.js-pagination

A simple utility (or advanced - information technology's your choice) to paginate discord embeds. Built on discord.js@^thirteen.0.0.

To see how the example paginations look, checkout the instance bot (the readme has gifs)!

Key Features

  • Custom emoji reactions with the ReactionPaginator (see the example app).
  • Custom button pagination with the ButtonPaginator (come across the example app).
  • Custom select carte pagination with the SelectPaginator (see the example app).
  • If you lot have a global interaction listener y'all can ignore paginator interactions by checking: interaction.customId.startsWith("paginator") - this prefix is customizable, set up it on all of your paginators.
  • Multiple Action Row (combined select and button) pagination!

Table of Contents

  • Installation
  • Updating v3 to v4
  • Basic Usage
  • Types
  • Paginator Properties
  • Paginator Options
  • Paginator Events

Installation

  • npm install @psibean/discord.js-pagination

Updating

To update from version 3 to version 4 you may want to accept a look at the difference between the two versions in the example app.

Still, for Uncomplicated cases, you probable just need to move your pages parameter into the options object in the constructor call, see the bones usage beneath!

If you want to make use of the dynamic embed creation yous'll desire to acquire how to apply the identifiersResolver and the pageEmbedResolver, read their descriptions below and check the example app for further sit-in.

Basic Usage

For all paginators, the default messageSender will telephone call interaction#editReply then return interaction#fetchReply, this means your control (as per the example bot) must phone call interaction#deferReply.

You should use interaction#deferReply when you receive an interaction that will transport an embed. The paginators default messageSender uses interaction#editReply. If y'all do not wish to exercise this you'll need to pass in your own messageSender.

For the below examples, the pages can be synthetic as per the example bot:

                const                pages                =                [                ]                ;                for                (                let                i                =                0                ;                i                ten                ;                i                ++                )                {                const                pageEmbed                =                new                MessageEmbed                (                )                ;                pageEmbed                .                setTitle                (                `This embed is index                                      ${                    i                    }                  !`                )                .                setDescription                (                `That means it is page #                    ${                    i                    +                    1                    }                  `                )                ;                pages                .                push                (                pageEmbed                )                ;                }              

You will of course want to construct your ain pages.

For more complex usage see the example bot.

ReactionPaginator

This allows pages to be navigated by reacting to a bulletin.

                const                {                PaginatorEvents,                ReactionPaginator                }                =                crave                (                '@psibean/discord.js-pagination'                )                ;                const                reactionPaginator                =                new                ReactionPaginator                (                interaction                ,                {                pages                }                )                .                on                (                PaginatorEvents                .                COLLECT_ERROR                ,                (                {                error                }                )                =>                console                .                log                (                error                )                )                ;                expect                reactionPaginator                .                send                (                )                ;              

ButtonPaginator

This allows pages to exist navigated past clicking buttons.

                const                {                ButtonPaginator                }                =                require                (                '@psibean/discord.js-pagination'                )                ;                const                buttonPaginator                =                new                ButtonPaginator                (                interaction                ,                {                pages                }                )                ;                await                buttonPaginator                .                transport                (                )                ;              

SelectPaginator

This allows pages to exist navigated using a select menu.

For the select pagination embed you'll need to supply an assortment of MessageSelectOptions to the pagination options via selectOptions. By default the pagination will map the value of the provided options to your pages index based on their ordering. And so the page change volition exist determined by the selected value and this mapping. If you desire to map your select options and pages differently you can provide the pagesMap ({ selectOptions, paginator }) function which should render a lexicon. You lot'll so want to provide a custom pageIndexResolver.

                const                {                SelectPaginator                }                =                require                (                '@psibean/discord.js-pagination'                )                ;                // The default pagesMap volition map these option values to the index                const                selectOptions                =                [                ]                ;                for                (                let                i                =                0                ;                i                <                10                ;                i                ++                )                selectOptions                .                push                (                {                label:                `"Page #                    ${                    i                    +                    ane                    }                  `                ,                value:                `                    ${                    i                    }                  `                ,                description:                `This will take you to page #                    ${                    i                    +                    ane                    }                  `                }                )                ;                const                selectPaginator                =                new                SelectPaginator                (                interaction                ,                pages                )                ;                expect                selectPaginator                .                send                (                )                ;              

Types

PaginatorIdentifiers

This object is returned past the identifiersResolver and can contain whatever number of identifiers simply at bare minimum information technology must contain a pageIdentifier.

{ ...args, pageIdentifier: string | number }

  • args: Any additional identifiers yous wish to use.
  • pageIdentifier: Used every bit a unique identifier to resolve a page

PaginatorCollectorArgs

{ ...args, paginator }

  • args: All arguments provided by the collect listener.
  • paginator : This is a reference to the paginator instance.

For the ReactionCollector this is:

              { reaction, user, paginator }                          

For ActionRowPaginator, ButtonPaginator and SelectPaginator this is:

              { interaction, paginator }                          

ChangePageArgs

{ collectorArgs, previousIdentifiers, currentIdentifiers, newIdentifiers, paginator }

  • collectorArgs: The PaginatorCollectorArgs - only in the collect event (not initial send).
  • previousIdentifiers: The PaginatorIdentifiers of the previous page. Empty object if no navigation.
  • currentIdentifiers: The PaginatorIdentifiers of the current page, before irresolute.
  • newIdentifiers: THe PaginatorIdentifiers of the page to exist inverse to.
  • paginator : This is a reference to the paginator instance.

Paginator Backdrop

The paginator has a variety of properties you tin acces -and should, especially if y'all're customising!

Properties can be accessed via paginator#propertyName

Base Properties

These properties are common to all paginators.

  • customer : The client that instantiated the paginator.
  • interaction : The interaction that initiated the instantiation of the paginator.
  • user : The user who sent the interaction that instantiated the paginator.
  • channel : The aqueduct where the interaction came from, this is as well where the paginator message will be sent.
  • pages : The cache of MessageEmbeds mapped by their respective PaginationIdentifiers#pageIdentifier.
    • If pages are provided in the options of the constructor, this volition be all of the pages provided mapped past their index.
  • initialIdentifiers : The initial PaginatorIdentifiers provided in the paginator options.
  • numberOfPages : The number of pages.
  • previousPageIdentifiers : The PaginatorIdentifiers from the previous navigation, naught indicates no page change yet.
  • currentPageIdentifiers : The PaginatorIdentifiers of the current page.
  • options : The options provided to the paginator after merging defaults.

ActionRowPaginator Properties

These backdrop are mutual to the ButtonPaginator and SelectPaginator.

  • customIdPrefix : The customIdPrefix provided in options. Used to prefix all MessageComponent#customId's.
  • customIdSuffix : The interaction#id. Used to suffix all MessageComponent#customId'south.

SelectPaginator Properties

These properties are specific to the SelectPaginator.

  • selectMenu : The select menu on the paginator message activity row.

Paginator Options

The post-obit options are available for the respective paginators.

Base Options

All paginators can have the options of their corresponding collector, see discord.js documentation for those. Instead of filter, provide collectorFilter. The options listed here are available to all paginators.

messageSender

The function used to send the intial folio, it returns the message.

({ interaction, messageOptions }): Promise<Message> | Message

  • interaction : This is a reference to the initial interaction that instantiated the paginator.
  • messageOptions : The MessageOptions for the initial page.

Defaults to:

                messageSender:                async                (                {                interaction,                messageOptions                }                )                =>                {                await                paginator                .                interaction                .                editReply                (                messageOptions                )                ;                return                paginator                .                interaction                .                fetchReply                (                )                ;                }              

pageEmbedResolver

This is used to determine the current pages embed based on the change page args.

(changePageArgs: ChangePageArgs): Promise<MessageEmbed> | MessageEmbed

If using cache this part will but be called if the embed isn't already in the cache and the embed will be added to the cache mapped to it'southward respective pageIdentifier.

useCache

Whether or not embeds should exist buried, keyed by their pageIdentifier.

Defaults to true.

maxPageCache

The maximum number of pages that should exist cached. If this number is reached the cache will be emptied before a new folio is cached.

Defaults to 100.

messageOptionsResolver

This function is used to determine additional MessageOptions for the page existence changed to.

(options: ChangePageArgs): Promise<MessageOptions> | MessageOptions

Defaults to returning an empty object. Optional.

collectorOptions

This is the options that will be passed to the collector when it is created.

For the ReactionPaginator, defaults to:

                {                idle:                6e4                ,                filter:                (                {                reaction,                user,                paginator                }                )                =>                user                ===                paginator                .                user                &&                paginator                .                emojiList                .                includes                (                reaction                .                emoji                .                name                )                &&                !                user                .                bot                ,                }              

For the ActionRowPaginator, ButtonPaginator and SelectPaginator, defaults to:

                {                idle:                6e4                ,                filter:                (                {                interaction,                paginator                }                )                =>                interaction                .                isMessageComponent                (                )                &&                interaction                .                component                .                customId                .                startsWith                (                paginator                .                customIdPrefix                )                &&                interaction                .                user                ===                paginator                .                user                &&                !                interaction                .                user                .                bot                ,                }                ,                }              

identifiersResolver

This is the function used to make up one's mind the new identifiers object based on the interaction that occurred. The object must incorporate a pageIdentifier but tin can otherwise incorporate annihilation else.

(collectorArgs: PaginatorCollectorArgs): Promise<PaginatorIdentifiers> | PaginatorIdentifiers

Whatsoever this role returns will be used as the newIdentifiers object for it's collect event.

For the ReactionPaginator, defaults to:

                (                {                reaction,                paginator                }                )                =>                {                let                {                pageIdentifier                }                =                paginator                .                currentIdentifiers                ;                switch                (                reaction                .                emoji                .                name                )                {                instance                paginator                .                emojiList                [                0                ]:                pageIdentifier                -=                1                ;                break                ;                example                paginator                .                emojiList                [                1                ]:                pageIdentifier                +=                1                ;                break                ;                }                // The default identifier is a cyclic index.                if                (                pageIdentifier                <                0                )                {                pageIdentifier                =                paginator                .                maxNumberOfPages                +                (                pageIdentifier                %                paginator                .                maxNumberOfPages                )                ;                }                else                if                (                pageIdentifier                >=                paginator                .                maxNumberOfPages                )                {                pageIdentifier                %=                paginator                .                maxNumberOfPages                ;                }                return                {                ...paginator                .                currentIdentifiers                ,                pageIdentifier                }                ;                }              

For the ButtonPaginator, defaults to:

                (                {                interaction,                paginator                }                )                =>                {                const                val                =                interaction                .                component                .                label                .                toLowerCase                (                )                ;                let                {                pageIdentifier                }                =                paginator                .                currentIdentifiers                ;                if                (                val                ===                'previous'                )                pageIdentifier                -=                one                ;                else                if                (                val                ===                'next'                )                pageIdentifier                +=                1                ;                // The default identifier is a cyclic alphabetize.                if                (                pageIdentifier                <                0                )                {                pageIdentifier                =                paginator                .                maxNumberOfPages                +                (                pageIdentifier                %                paginator                .                maxNumberOfPages                )                ;                }                else                if                (                pageIdentifier                >=                paginator                .                maxNumberOfPages                )                {                pageIdentifier                %=                paginator                .                maxNumberOfPages                ;                }                return                {                ...paginator                .                currentIdentifiers                ,                pageIdentifier                }                ;                }              

For the SelectPaginator, defaults to:

                (                {                interaction,                paginator                }                )                =>                {                const                [                selectedValue                ]                =                interaction                .                values                ;                return                {                ...paginator                .                currentIdentifiers                ,                pageIdentifier:                parseInt                (                selectedValue                )                }                ;                }                ,              

shouldChangePage

This is the function used to make up one's mind whether or not a page modify should occur during a collect event. If not provided, a page change will always occur during a collect event.

(args: ChangePageArgs): Promise<boolean> | boolean

Defaults to:

                (                {                newIdentifiers,                currentIdentifiers,                paginator                }                )                =>                paginator                .                message                .                deletable                &&                newIdentifiers                .                pageIdentifier                !==                currentIdentifiers                .                pageIdentifier                ,              

footerResolver

This is the function used to gear up the footer of each page. If non provided the embeds will apply whatever footers were originally assail them.

(paginator): Promise<cord> | string

  • paginator : This is a reference to the paginator instance.

Defaults to undefined. Optional.

initialIdentifiers

This is the initial PageIdentifiers which is passed in as the newIdentifiers in the very showtime ship.

ReactionPaginator Options

These options are specific to the ReactionPaginator.

emojiList

An assortment of emoji resolvables to use as reactions.

Defaults to: ['', '']

ActionRowPaginator Options

These options are mutual to the ActionRowPaginator, ButtonPaginator, and SelectPaginator.

messageActionRows

An array of messageActionRows (they will accept their type set to ACTION_ROW). Whatever components provided up forepart will take their customId generated.

Defaults to:

              [ 	{ 		type: 'ACTION_ROW', 		components: [], 	}, ],                          

customIdPrefix

This is used to prefix all of the MessageComponent#customId's.

Defaults to: "paginator"

ButtonPaginator Options

These options are specific to the ButtonPaginator.

buttons

An array of MessageButtonOptions which will exist added to the paginators activity row. Tin can exist skipped if you're degining the ActionRowPaginator#messageActionRows selection. Otherwise these will be added to either the first activity row, or added based on a row property.

Annotation:

  • customId is not required, the paginator will update the customId to be: <prefix>-[<customId>-]<suffix> where the suffix is the id of the received interaction that initiated the paginator.
  • type volition exist updated to Push button
  • style will default to Principal if not set

Defaults to:

                [                {                characterization:                'Previous'                ,                }                ,                {                label:                'Adjacent'                ,                }                ]                ,              

SelectPaginator Options

These options are specific to the SelectPaginator.

selectOptions

An array of MessageSelectOptions to exist added to the select menu.

Note:

  • customId is non required, the paginator will update the customId to exist: <prefix>-[<customId>-]<suffix> where the prefix is paginator#customIdPrefix and the suffix is the id of the received interaction that initiated the paginator.

pagesMap

A function that returns a lexicon, or a dictionary. The dictionary should provide a style to map the provided selectOptions to your provided pages.

If a office:

({ selectOptions, paginator}): Dictionary<K, V>

  • selectOptions : The selectOptions provided to the paginator later having their customId updated.
  • paginator : This is a reference to the paginator instance.

Paginator Events

Events tin can be imported by:

              const { PaginatorEvents } = require('@psibean/discord.js-pagination');                          

And accessed past PaginatorEvents#EventName

To listen to an event:

              paginator.on(PaginatorEvents#EventName, eventHandler);                          

All paginators take the post-obit events (by EventName):

BEFORE_PAGE_CHANGED

'beforePageChanged'

This issue is raised in the paginator collectors collect effect earlier the message is edited with a new page.

Parameters: ({ ...*, newPageIdentifier, currentPageIdentifier, paginator })

  • ...* : Includes any args provided by the collect listener.
  • newPageIdentifier : This is the index of the page being changed to.
  • currentPageIdentifier : This is the index of the electric current page (before being changed).
  • paginator : This is a reference to the paginator instance.

COLLECT_END

'collectEnd'

This upshot is raised at the terminate of each collect handled by the paginator collector. Notation that this won't be called if an fault is thrown or if the paginator does non modify page. Employ the COLLECT_ERROR and PAGE_CHANGED events respectively in those cases.

Parameters: ({ error, paginator })

  • error : This is the error that was defenseless.
  • paginator : This is a reference to the paginator instance.

COLLECT_ERROR

'collectError'

This event is raised when an fault occurs within the collect outcome of the paginators collector.

Parameters: ({ error, paginator })

  • error : This is the error that was defenseless.
  • paginator : This is a reference to the paginator instance.

COLLECT_START

'collectStart'

This event is raised when an error occurs within the collect event of the paginators collector.

Parameters: ({ error, paginator })

  • fault : This is the error that was caught.
  • paginator : This is a reference to the paginator instance.

PAGE_CHANGED

'pageChanged'

This upshot is raised in the paginator collectors collect issue after the bulletin is edited with a new page

Parameters: ({ ...*, newPageIdentifier, currentPageIdentifier, paginator })

  • ...* : Includes any args provided by the collect listener.
  • newPageIdentifier : This is the alphabetize of the page beingness changed to.
  • currentPageIdentifier : This is the index of the electric current folio (before beingness changed).
  • paginator : This is a reference to the paginator instance.

It should be noted: paginator#currentPageIdentifier, paginator#previousPageIdentifier and paginator#currentPage will at present reflect values based on the newPageIdentifier, which is not the case for BEFORE_PAGE_CHANGED.

PAGE_UNCHANGED

'pageUnchanged'

This event is raised in the paginator collectors collect event when paginator#shouldChangePage has returned false. For instance if the new page index matches the current alphabetize, a collect event occurred but did non cause a page alter.

Parameters: ({ ...*, newPageIdentifier, currentPageIdentifier, paginator })

  • ...* : Includes any args provided by the collect listener.
  • newPageIdentifier : This is the index of the page being changed to.
  • currentPageIdentifier : This is the index of the current page (before being changed).
  • paginator : This is a reference to the paginator case.

PAGINATION_END

'paginationEnd'

This event is raised in the paginators collector finish consequence.

Parameters: ({ collected, reason, paginator })

  • nerveless : The elements collected by the paginators collector.
  • reason : The reason the paginators collector ended.
  • paginator : This is a reference to the paginator instance.

PAGINATION_READY

'paginationReady'

This result is raised once the paginators collector has been setup and the message has been sent with the starting page.

Parameters: (paginator)

  • paginator : This is a reference to the paginator instance.

rogertheept.blogspot.com

Source: https://www.npmjs.com/package/@psibean/discord.js-pagination

0 Response to "How to Read Count of Emoji Collectory Discord.js"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel