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

v0.3.5

Published

This is a minimal library intended to make it easier to build serverless Discord bots to be hosted on Worker Runtime services like [Cloudflare Workers](https://workers.cloudflare.com).

Downloads

5

Readme

discord-workers

This is a minimal library intended to make it easier to build serverless Discord bots to be hosted on Worker Runtime services like Cloudflare Workers.

The present features in this library are usable, but does not include full support for all types of Interactions. I will continue developing this further as I use it to re-write one of my own Discord bots (Manatee) to function on CF workers. Full docs, examples, and API reference will be released soon.

Installation

Shocker.

npm i discord-workers

If you're working in TypeScript, it'll also be tremendously helpful to additionally install discord-api-types.

Usage

You can create a handler method that functions as a Module Worker fetch() function. You'll have to pass in an array of commands with this format, plus an execute() method as shown.

import { createHandler } from "discord-workers";

const commands = [
  {
    name: "echo",
    description: "Responds with whatever you say.",
    options: [
      { name: "text", type: 3, description: "Text to echo", required: false },
    ],
    execute(int) {
      const value = int.getOption("text") ?? "Hello World!";
      return int.send(value);
    },
  },
  {
    name: "api",
    description: "Generic command that queries some API",
    execute(int, env, ctx) {
      ctx.waitUntil(
        (async () => {
          const resp = await fetch("https://random-data-api.com/api/v2/users");
          const body = await resp.json();
          return int.send(
            `Random person I made up: ${body.first_name} ${body.last_name}`
          );
        })()
      );
      return int.defer();
    },
  },
];

export default {
  fetch: createHandler(commands),
};

TypeScript usage

Here's the same example as above, but written in TypeScript. Since discord-workers is written for TS first, it provides tons of useful typings for its built-in methods, and it plays nicely with discord-api-types.

Also note that you'll have to provide Wrangler secrets for applicationID and publicKey in order for the handler to function properly. This is enforced by the module's typings.

Optionally, you can also provide a botToken secret, which will allow you to sync all commands with Discord by going to PUT /sync. If you don't provide this secret, you can still execute the endpoint but provide an Authorization header with the bot token. For security reasons, it is recommended that you use this method in production and use the botToken secret for development ONLY.

import { Command, CommandInteraction, createHandler } from "discord-workers";

interface Env {
  applicationID: string;
  publicKey: string;
}

const commands: Command[] = [
  {
    name: "echo",
    description: "Responds with whatever you say.",
    options: [
      { name: "text", type: 3, description: "Text to echo", required: false },
    ],
    execute(int: CommandInteraction): Promise<Response> {
      const value = int.getOption<string>("text") ?? "Hello World!";
      return int.send(value);
    },
  },
  {
    name: "api",
    description: "Generic command that queries some API",
    execute(int: CommandInteraction, env: Env, ctx: ExecutionContext) {
      ctx.waitUntil(
        (async () => {
          const resp = await fetch("https://random-data-api.com/api/v2/users");
          const body = await resp.json();
          return int.send(
            `Random person I made up: ${body.first_name} ${body.last_name}`
          );
        })()
      );
      return int.defer();
    },
  },
];

export default {
  fetch: createHandler<Env>(commands),
};