npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@fyleto/dpager

v1.0.0

Published

Discord.js Pager

Downloads

5

Readme

dpager

Discord.js Pager

Install

npm i @fyleto/dpager
yarn add @fyleto/dpager

Use

const { Pager } = require("@fyleto/dpager");
import { Pager } from "@fyleto/dpager";

Pager

Class that defines the page handler.

Constructor

  • title (optional): Default title to use for pages that do not have it's own title
const { Pager } = require("@fyleto/dpager");

const pages = new Pager("shop");

Methods

addPage

Add a singular page.

  • T : Content of the page (string)
  • T (overload) : Content, title and others for the page's embed (object)
const pages = new Pager();

pages.addPage("what's up"); // add page a from string
pages.addPage({
    title: "Page title",
    content: "Page content",
}); // add a page from an object

addPages

Add multiple pages at once

  • ...T: Content of the page (string)
  • ...T (overload) : Content, title and others for the page's embed (object)
const pages = new Pager();

pages.addPages("what's up", "my name's ko!"); // add page a from string
pages.addPages(
    {
        title: "Page title",
        content: "Page content",
    },
    {
        title: "goofy",
        content: "mhm",
    }
); // add a page from an object

removePage

Remove one page by it's index

  • index: Page index (0-indexed, so page 1 is 0)
pages.removePage(1); // removes page 2

addDynamicPages

Dynamically pages from an array of strings

  • array: Array of strings
  • maxContentperPage: Amount of content on a page before it goes to the next page
  • seprator(optional): Seprator to seprate content on one page
pages.addDynamicPages(["hi", "im", "dy", "namic!", "cool?"], 2, "\n"); // separator defaults to a line break. I just addedd for example's sake

// equavalent to

pages.addPages("hi\nim", "dy\nnamic!", "cool?");

config

Change the buttons. (You cannot edit the customIds and it's recommended you don't or else this won't work)

  • i: Object of new buttons (Should look something like this):
const {ButtonStyle} = require("discord.js")

let buttons = {
    nextPage: {}, // button to go over to the next page
    prevPage: {}, //  button to go to the previous page
    prevMaxPage: {}, // button to skip to the first page
    nextMaxPage: {} // button to skip to the last page
}

// and those objects can accept these following parameters:

{
    label: "Button label",
    style: ButtonStyle.Success,
    emoji: "Button emoji"
}

pages.config(i)

currentPage

Returns an object with the current page's embed, components and raw contents if you do not want it's embed

  • customId (optional): When given the customId, it will go to the page based on the id (So if you pass "nextPage" into it, It will change the index and return the object for the next page. and etc)
pages.currentPage(); // on page 1, returns page 1 contents
pages.currentPage("nextPage"); // on page 2 now, returns page 2 contents
pages.currentPage(); // still on page 2
pages.currentPage("prevPage"); // back on page 1, returns it's contents now

returns

{
    embed: // instance of EmbedBuilder that you can add to an interaction.reply/update,
    components: // array of components that contains the required buttons
    raw: {
        title: // page's title if given. If not, is undefined
        content: // page's content.
    }
}

Properties

pages

Array of the pages.

pages.pages;

index

Current page index. (0-indexed)

pages.index;

Example

const { Pager } = require("@fyleto/dpager");
const { ComponentType, ButtonStyle } = require("discord.js");

// place this code either in an "interactionCreate" event or any slash command event that takes interaction as a parameter

const pager = new Pager();

pager.addDynamicPages(
    [
        "This page content",
        "is seprated",
        "wow",
        "isnt that cool?",
        "no",
        "it's not cool :pensive:",
        "Yes you nerd.",
    ],
    2
);

// We don't define the labels, so the default labels (arrows) are used.
pager.config({
    nextPage: {
        style: ButtonStyle.Primary,
    },
    prevPage: {
        style: ButtonStyle.Primary,
    },
    nextMaxPage: {
        style: ButtonStyle.Danger,
    },
    prevMaxPage: {
        style: ButtonStyle.Danger,
    },
});

let initPage = await pager.currentPage();
let index = `Page ${pager.index + 1}/${pager.pages.length}`;

await interaction.reply({
    content: index,
    embeds: [initPage.embed],
    components: initPage.components,
});

const reply = await interaction.fetchReply();

const collector = reply.createMessageComponentCollector({
    componentType: ComponentType.Button,
    time: 15_000,
});

collector.on("collect", async (i) => {
    let page = await pager.currentPage(i.customId);
    let index = `Page ${pager.index + 1}/${pager.pages.length}`;
    i.update({
        content: index,
        embeds: [page.embed],
        components: page.components,
    });
});

Do not mind my chromebook's bad quality