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-better-components

v1.0.2

Published

A package that simplifies the use of message components

Downloads

4

Readme

THIS PACKAGE IS NOT FINISHED! DO NOT USE IT FOR PRODUCTION

discord.js-better-buttons

About

discord.js-better-components is a package that makes message components easier (and fun) to use.

Installation

Node.js 16.9.0 or newer is required.

npm i discord.js discord.js-better-components

Fundamentals

With discord.js-better-components, five main "elements" are used:

  • Button: Your standard message button
  • Menu: Your standard select menu
  • Menu Option: An option for a select menu
  • Button Bundle: A container for buttons (minimum of 1 button and maximum of 5)
  • Package: A container for button bundles and select menus (minimum of 1 element and maximum of 5)

When a button is clicked, it emits a "click" event with the MessageComponentInteraction passed as an argument.

When a select menu option is clicked, it emits a "click" event and passes the MessageComponentInteraction as well.

Example Usage

const { Button, ButtonStyles, ButtonBundle, Menu, MenuOption, Package } = require('discord.js-better-components');

const button = new Button({ label: 'My button', style: ButtonStyles.primary });
const bundle = new ButtonBundle([ button ]);

const option1 = new MenuOption({ label: 'Apples are better than oranges', description: 'If you say so...' });
const option2 = new MenuOption({ label: 'Oranges are better than apples', description: 'If you say so...' });
const menu = new Menu({ placeholder: 'Nothing selected', options: [ option1, option2 ]});

const package = new Package([ bundle, menu ], interaction.client);

button.on('click', i => console.log('My button was clicked!'));

option1.on('click', i => console.log(`${i.user.username} thinks that apples are better than oranges!`));
option2.on('click', i => console.log(`${i.user.username} thinks that oranges are better than apples!`));

await interaction.reply({ content: 'Here\'s a button!', components: package.format() });

Alternatively, if you're a masochist, here's how you do that exact same thing with discord.js:

const row = new Discord.MessageActionRow()
	.addComponents(
		new Discord.MessageButton()
			.setLabel('My button')
			.setCustomId('mybutton')
			.setStyle('PRIMARY'),
	);

const row2 = new MessageActionRow()
	.addComponents(
		new MessageSelectMenu()
			.setCustomId('select')
			.setPlaceholder('Nothing selected')
			.addOptions([
				{
					label: 'Apples are better than oranges',
					description: 'If you say so...',
					value: 'apple_option',
				},
				{
					label: 'Oranges are better than apples',
					description: 'If you say so...',
					value: 'orange_option',
				},
			]),
	);

await interaction.reply({ content: 'Here\'s a button!', components: [row, row2] });

const filter = i => i.user.id == interaction.user.id;

const collector = interaction.channel.createMessageComponentCollector({ filter, time: 15000 });

collector.on('collect', i => {
	if (i.customId == 'mybutton') {
		console.log('My button was clicked!');
	}
    else if (i.customId == 'select') {
        const optionPicked = i.values[0];
        if (optionPicked == 'apple_option') {
            console.log(`${i.user.username} thinks that apples are better than oranges!`);
        }
        else if (optionPicked == 'orange_option') {
            console.log(`${i.user.username} thinks that oranges are better than apples!`);
        }
    }
});

Yeah, it's not pretty. Having to handle custom ID's, the huge constructors for action rows and components... it makes me shiver just thinking about it.