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

discord.js-message-listener

v3.0.3

Published

A simple utility to bind events on discord message.

Downloads

117

Readme

discord.js-message-listener

Codacy Badge codecov Issue Badge Forks Badge Stars Badge License Badge

A simple utility to bind events on discord message.

You can use this library to listen message interaction, and react to it. For example, you can do a embed message with pagination, or a selector to choose a option.

Features

  • Listen message component interaction
    • Button
    • Select menu
  • High flexibility and Extentable
    • Custom message content and component
    • Mulitple action row
  • With simple utility
    • Paginator

Installation

# if you are using discord.js v13
npm i discord.js-message-listener@^2.0.0
# v14
npm i discord.js-message-listener

Example

Examples can be found in example bot.

Usage

Listener

All listener provide a same interface to operate. You can use it like this:

// it is psudocode
const listener = new Listener(message, { collectorOptions });
listener.on("collect", (arg) => console.log("collect", arg));
listener.on("collectError", (error) => console.log(error));
listener.on("end", (collected, reason) => console.log("listenerEnd"));
await listener.start(); // it would perform some required action

ActionRowMessageListener

import { ActionRowMessageListener } from "discord.js-message-listener";

ActionRowMessageListener provide a simple way to create action rows and listen to them. To use it, you need to pass a message, and a list of action row to it. It will edit the message to show the action row and listen to it.

const messageActionRow = new ActionRowBuilder();
const btn1 = new ButtonBuilder();
const btn2 = new ButtonBuilder();
btn1.setLabel("1");
btn2.setLabel("2");
messageActionRow.addComponents(btn1, btn2);
const listener = new ActionRowMessageListener(message, {
  messageActionRows: [messageActionRow],
  collectorOptions: {
    time: 10000,
  },
});
listener.on(
  "collect",
  // use as to cast to subclass of Interaction
  (arg) => console.log((arg as ButtonInteraction).customId)
);
await listener.start();

Utility

Paginator

import { Paginator } from "discord.js-message-listener";

Paginator provide a simple way to create paginator. It need a message listener to listen to the message. The other thing you need to do is to pass filter functions to it. Because the paginator accept any listener, you can use it flexibly.

The paginator will bind collect event to the listener for you, so you don't need to do it manually.

const messageActionRow = new ActionRowBuilder();
const prevPage = new ButtonBuilder();
const nextPage = new ButtonBuilder();
prevPage.setLabel("Prev");
prevPage.setCustomId("prev-btn");
nextPage.setLabel("Next");
nextPage.setCustomId("next-btn");
messageActionRow.addComponents(prevPage, nextPage);
const listener = new ActionRowMessageListener(msg, {
  messageActionRows: [messageActionRow],
  collectorOptions: {
    time: 60000,
  },
});
const page1 = new EmbedBuilder();
const page2 = new EmbedBuilder();
page1.setTitle("Page 1");
page2.setTitle("Page 2");
const pages = [page1, page2];
const paginator = new Paginator(listener, {
  pages,
  nextPageFilter: (arg) => (arg as ButtonInteraction).customId === "next-btn",
  previousPageFilter: (arg) =>
    (arg as ButtonInteraction).customId === "prev-btn",
});
// you should call start() to start the paginator
// start() will call listener.start() for you
await paginator.start();

You can also bind error handler and end handler to the paginator.

Error handler and end handler will be bind to the listener.

const paginator = new Paginator(listener, {
  pages,
  nextPageFilter: (arg) => (arg as ButtonInteraction).customId === "next-btn",
  previousPageFilter: (arg) =>
    (arg as ButtonInteraction).customId === "prev-btn",
  errorHandler: (error) => {
    // to something
    console.log(error);
  },
  endHandler: (collected, reason) => {
    // to something
    console.log(reason);
  },
});