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-prompts

v3.0.0

Published

Create prompts in Discord, just like you would in console!

Downloads

68

Readme

Discord.js Prompts

Maintainability Test Coverage Github license

Create prompts in Discord, just like you would in console! Implemented with prompt-anything.

For full documentation, see prompt-anything#usage's documentation and the example below (available in JS and TS). It comes with all the same features of prompt-anything, including

  • Modular, reusable prompts that are also composed of reusable components
  • Conditional execution in whatever order you can dream of
  • Trivial unit tests and (mostly) trivial integration tests (see prompt-anything#testing)
  • Channel tracking that can be used to prevent multiple prompts running within the same channel
  • Built-in support for multiple choice menus with pagination and time limits

Table of Contents

Example

See the ./examples folder. Both TypeScript and JavaScript are provided.

This will cause the bot application (with discord.js) to ask the user for their name and age. The bot will then send the collected results back. An image of the interaction is provided at the bottom.

Using Menus

The built-in menus will automatically handle invalid options and numbering. You simply use the pre-made visual components of MenuEmbed with the MenuVisual.

Automatic pagination controls via reactions is also built in (see the next section)!

import { MessageEmbed } from 'discord.js'
const embed = new MessageEmbed({
  title: 'What is your favorite fruit?'
})
const askFruitMenu = new MenuEmbed(embed)
  .addOption('Apple')
  .addOption('Orange')
  .addOption('Broccoli', 'Broccoli is so tasty, it might as well be a fruit')
const askFruitVisual = new MenuVisual(askFruitMenu)

const askFruitFn: DiscordPromptFunction<AgeMenuData> = async (message: Message, data: AgeMenuData) => {
  const { content } = message
  if (content === '1') {
    // apple
  } else if (content === '2') {
    // orange
  } else {
    // broccoli
  }
  return data
}

const askFruitPrompt = new DiscordPrompt<AgeMenuData>(askFruitVisual, askFruitFn)
const askFruitNode = new PromptNode(askFruitPrompt)

Pagination

Pagination is disabled by default. To enable it, pass a callback to MenuEmbed.prototype.enablePagination that will handle any errors that occurs from adding reactions or editing the message.

You can also pass a maxPerPage or paginationTimeout (time until reactions are no longer accepted) in an object as the second argument (the first argument is to initialize it with a pre-made embed).

import { MessageEmbed } from 'discord.js'
const embed = new MessageEmbed({
  title: 'What is your favorite fruit?'
})
const askFruitMenu = new MenuEmbed(embed, { maxPerPage: 2 })
  .addOption('Apple')
  .addOption('Orange')
  .addOption('Broccoli', 'Broccoli is so tasty, it might as well be a fruit')
  .enablePagination((error: Error) => {
    // Handle errors here
    throw error
  })

Channel Tracking

Every time prompts are run with DiscordPromptRunner, the channel ID is stored in a Set that is then removed upon prompt completion.

You can access these for your convenience through the static methods:

DiscordPromptRunner.addActiveChannel(id: string): void
DiscordPromptRunner.deleteActiveChannel(id: string): void
DiscordPromptRunner.isActiveChannel(id: string): boolean

You can call isActiveChannel before calling a DiscordPromptRunner's run method for example.