Skip to content
This repository has been archived by the owner on May 27, 2022. It is now read-only.

A boilerplate template for Cloudflare Workers to Respond to Discord Slash Commands serverlessly

License

Notifications You must be signed in to change notification settings

judge2020/cloudflare-worker-discord-interactions-template

Repository files navigation

cloudflare-worker-discord-interactions-template

An early-stage Discord Interactions template for Cloudflare Workers via Wrangler. Running completely serverless via Cloudflare provides many benefits and makes running a slash command bot basically zero-maintenance.

While this is a good starting point for slash commands, it is not meant to be a 'framework' - it only goes as far as providing examples and some helper functions. You most certainly will need to refer to the official Discord API docs to properly utilize slash commands.

🔋 Getting Started

This template is meant to be used with Wrangler. If you are not already familiar with the tool, we recommend that you install the tool and configure it to work with your Cloudflare account. Documentation can be found here.

To generate using Wrangler, run this command:

wrangler generate my-discord-project https://github.com/judge2020/cloudflare-worker-discord-interactions-template

Once created, edit wrangler.toml:

  • set account_id to the account ID which will house the worker (obtain from here)
  • change name to a name that works for you and represents your application. If you use a workers.dev subdomain (which is perfectly fine to do), this will be the subdomain, ie naming it test-discord will have the endpoint end up as test-discord.my-username.workers.dev)

Client setup

If you don't have a Discord client already, go to the Discord developer applications page and create one.

Run the following commands based off of the info listed in the 'general information' section of your application:

wrangler secret put CLIENT_PUB_KEY
wrangler secret put CLIENT_SECRET
wrangler secret put CLIENT_ID

In production, it is recommended to set up CONFIG_KV_NAMESPACE so that it caches the client credential grant token that is used for command management API calls. To do so, run wrangler kv:namespace create CONFIG_KV_NAMESPACE and copy the text it provides to your wrangler.toml.

These will be exposed to the application as global variable strings for development and are all required.

After setting up your application and deploying (see below), point the 'interactions url' in the discord app settings to {your endpoint}/interactions, eg https://test-discord.my-username.workers.dev/interactions).

👩 💻 Developing

src/handler.ts contains the main logic for commands - this function is pre-authenticated (see src/index.ts) so all you need to worry about is registering commands (this is planned to show up in this template in the future) and handling the different inputs.

🧪 Testing

Note: tests have not been modified for this template at the moment. npm test will run your tests.

✏️ Formatting

This template uses prettier to format the project. To invoke, run npm run format.

👀 Previewing and Publishing

For information on how to preview and publish your worker, please see the Wrangler docs.

Note that it is perfectly fine to run an interactions endpoint on a workers.dev subdomain.

🤢 Issues

If you run into issues with this specific project, please feel free to file an issue here. If the problem is with Wrangler, please file an issue here.

⚠️ Caveats

The service-worker-mock used by the tests is not a perfect representation of the Cloudflare Workers runtime. It is a general approximation. We recommend that you test end to end with wrangler dev in addition to a staging environment to test things before deploying.

About

A boilerplate template for Cloudflare Workers to Respond to Discord Slash Commands serverlessly

Resources

License

Stars

Watchers

Forks