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

slashy

v1.0.8

Published

An easy to use wrapper for Discord slash commands

Downloads

1

Readme

Slashy is an easy to use framework for Discord slash commands. It works with both interactions received over the gateway and via webhook, and even includes middleware to handle webhook validation with ease.

Installation

  1. Install
npm i slashy
# or
yarn add slashy
  1. Import
const {Interaction, validateInteraction, validateRequest} = require('slashy');
import {Interaction, validateInteraction, validateRequest} from 'slashy';

Methods

The following methods can be called on an instance of the Interaction class (defined i in the above examples)

constructor

The constructor only has one required parameter, interaction, containing interaction data. If you choose to exclude all other parameters, you must set your application ID as an environment variable called APPLICATION_ID and the default send options will be used.

  • new Interaction(interaction: Interaction)
  • new Interaction(interaction: Interaction, defaults?: Partial<InteractionResponse>)
  • new Interaction(interaction: Interaction, applicationId?: string, defaults?: Partial<InteractionResponse>)

Example:

const i = new Interaction(interaction, 'application-id', {
  type: 3 // send initial response as `ChannelMessage` by default
});

send

  • i.send(body: string): Promise<void | WebhookPostResult> - send a plain text message
  • i.send(body: InteractionResponse): Promise<void> - send the initial response message. Note: no data is returned
  • i.send(body: WebhookBody): Promise<WebhookPostResult> - send a followup message

edit

Exclude the id parameter to edit the initial response.

  • i.edit(body: string, id?: string): Promise<WebhookPostResult> - edit the message's text content
  • i.edit(body: WebhookBody, id?: string): Promise<WebhookPostResult> - edit the full message object

delete

Exclude the id parameter to delete the initial response.

  • i.delete(): Promise<void> - delete the initial response
  • i.delete(id: string) - delete a followup message

toString

  • i.toString(): string - generate the command that was typed by the user using the data provided

content (getter)

Alias for Interaction#toString

callbackURL (getter)

  • i.callbackURL: string - the callback URL (used for sending the initial response)

webhookURL (getter)

  • i.webhookURL: string - the webhook URL (used for sending followup messages)

Types

Slashy comes with all documented interaction types from Discord's API docs.

Usage

Slash supports both methods of receiving Discord interaction data.

For more examples, refer to the examples folder

Gateway

If you are using a bot, receiving interactions over the Discord gateway is the simplest option. Most Discord libraries have a way to listen for raw websocket events, so we'll use that to listen for INTERACTION_CREATE events.

Discord.js

bot.ws.on('INTERACTION_CREATE', interaction => {
  const i = new Interaction(interaction, bot.user.id);
});

Eris

bot.on('rawWS', event => {
  if (event.t === 'INTERACTION_CREATE') {
    const i = new Interaction(event.d, bot.user.id);
  }
});

Webhook

If you are not using a bot or prefer to receive interactions via webhook, theres a few additional steps you must take. Out of the box, slashy includes Express middleware to handle webhook validation as well as a validation function that makes it easy to use with any webserver.

Express

app.use(express.json());

app.post('/cmds', validateInteraction('public-key'), (req, res) => {
  const i = new Interaction(req.body, 'application-id');
});

Other Webservers

Not everyone uses express, there's some other great options out there. Slash comes with a validation function that makes verifying requests easy, no matter which framework you're using. In the example below, I will be using Fastify, but it should be pretty easy to use with whatever framework you're using.

fastify.post('/cmds', async (request, reply) => {
  const isSigned = await validateRequest(
    'public-key',
    request.body,
    request.headers
  );

  // request is not valid
  if (!isSigned) {
    return reply.code(401);
  }

  // acknowledge ping
  if (request.body.type === 1) {
    return {type: 1};
  }

  const i = new Interaction(request.body, 'application-id');
});