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

@magicyan/discord-ui

v0.2.0

Published

Discord UI

Downloads

46

Readme

Magicyan Discord Ui

Install with

npm install @magicyan/discord-ui
pnpm install @magicyan/discord-ui
yarn add @magicyan/discord-ui
bun install @magicyan/discord-ui

This lib provides customized "components" for your discord bot, see how to use it:

Get started

  • You can customize the buttons for all components of this lib using the discordUi function in your index file
import { discordUi } from "@magicyan/discord-ui";

discordUi({
  prompts: {
    confirm: {
      buttons: {
        confirm: { label: "Confirm", style: ButtonStyle.Success },
        cancel: { label: "Cancel", style: ButtonStyle.Secondary }
      }
    }
  },
  menus: {
    pagination: {
      buttons: {
        previous: { style: ButtonStyle.Danger },
        home: { label: "Home", emoji: "🏠" },
        next: { label: "Next", style: ButtonStyle.Success },
        close: { emoji: "❌" }
      }
    }
  }
});
  • This is applied to all components, but is overridden by customization in the component function itself if defined

Prompts

Confirm prompt

  • Easily create a component that waits for user confirmation
import { confirm } from "@magicyan/discord-ui";

confirm({
  components: ({ confirm, cancel }) => [
    new ActionRowBuilder({ components: [confirm, cancel] })
  ],
  render: components => interaction.reply({ 
    ephemeral: true, fetchReply: true, components,
    content: "Do you want to confirm this action?" 
  }),
  async onClick(interaction, isCancel) {
    await interaction.update({ components: [] });
    if (isCancel){
      interaction.editReply({ content: "This action has been canceled" });
      return;
    }
    interaction.editReply({ content: "Confirmed!" });
  },
});
  • You can customize the buttons
confirm({
  // ...
  buttons: {
    confirm: { label: "Continue", style: ButtonStyle.Primary },
    cancel: { label: "Cancel" },
  },
  render: components => interaction.reply({ 
    ephemeral: true, fetchReply: true, components,
    content: "Do you want to continue?" 
  })
});
  • You can specify a timeout and a function that will run when the time is up
confirm({
  // ...
  time: 60000,
  onTimeout() {
    interaction.editReply({ 
      content: "Time's up", 
      components: [] 
    });
  },
});
  • It is important that the render method receives a message, so if you are replying to interactions, set fetchReply to true
  • This prompt uses a discord collector, it will remain active until one of the buttons is clicked or the timeout -You can use the filter method if you are not sending ephemeral messages (it is the same as the discord component collector)

Menus

Pagination menu

  • This is a menu of embeds with pages, you just need to pass an array of embeds that you want to display
import { pagination } from "@magicyan/discord-ui";

pagination({
  embeds: [embed1, embed2, embed3],
  components: ({ home, close, next, previous }) => [
    new ActionRowBuilder<ButtonBuilder>({
        components: [previous, home, next, close]
    })
  ],
  render: (embed, components) => interaction.reply({ 
      fetchReply: true, ephemeral: true, embeds:[embed], components 
  }),
  onClick(interaction, embed) {
      interaction.reply({ ephemeral: true, embeds: [embed] });
  }
});
  • Working smart, try to map out whatever list structure you have
const roles = Array.from(interaction.member.roles.cache.values());

pagination({
  // ...
  embeds: roles.map((role, index) => new EmbedBuilder({
    description: `${role} ${role.name}`,
    color: role.color,
    footer: { text: `${index+1}/${roles.size}` }
  })),
});
  • Use the buttons to move forward and backward through the pages

  • You can customize each individual button using the buttons property

  • It uses a discord component collector, so you can pass a filter, a timeout and a function for when the time is up

  • You can set an action button if you want, along with its function, which when executed, does not change the pages but returns the embed of the current page for you to do whatever you want.


pagination({
  // ...
  components: ({ action, close, ...buttons }) => [
    new ActionRowBuilder<ButtonBuilder>({
        components: [...Object.values(buttons)]
    }),
    new ActionRowBuilder<ButtonBuilder>({
        components: [action, close]
    })
  ],
  // ...
  onClick(interaction, embed){
    interaction.reply({ ephemeral: true, embeds: [embed] })
  }
});
  • The close button causes the message to be deleted, but you can do whatever you want by defining the onClose method
pagination({
  // ...
  onClose(interaction){
    interaction.update({ 
      components: [], embeds: [], 
      content: "Closed" 
    })
  }
});