0.3.5 • Published 1 year ago

discord-workers v0.3.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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),
};
0.3.0

2 years ago

0.3.5

1 year ago

0.3.2

2 years ago

0.3.4

1 year ago

0.3.3

1 year ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.16

2 years ago

0.1.15

2 years ago

0.1.14

2 years ago

0.1.13

2 years ago

0.1.12

2 years ago

0.1.11

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago