Carbon

Cloudflare Workers

Learn how to set up and deploy your Carbon bot using Cloudflare Workers, including development and production environments.

Setup

Manual Setup

This is a continuation of the Basic Setup guide. If you haven't already, make sure to follow the steps in the guide before proceeding.

Create a Handler

Using the @buape/carbon/adapters/cloudflare package, you can create a handler that you can then export for Cloudflare Workers. This server will handle incoming interactions and route them to your bot.

import { createHandler } from '@buape/carbon/adapters/cloudflare'
 
const handle = createHandle( ... )
 
const handler = createHandler(handle)
export default { fetch: handler }

Running in Development

Set Environment Variables

First things first, you'll need to grab your Discord application's secrets from the Developer Portal and paste them in your .dev.vars file.

Start a Proxy

Discord requires a public URL to route interactions to your project. To achieve this, you'll need to set up a proxy. The simplest way to do this is by using localtunnel. Once you have the public URL, you may want to set it as BASE_URL="<PUBLIC_URL>" in your .dev.vars file.

npx localtunnel

You can use the --subdomain flag to specify a custom subdomain for your proxy.

Configure Portal URLs

Now that you have a public URL, navigate back to the Discord Developer Portal and set the "Interactions Endpoint URL" to <BASE_URL>/interactions.

Invite your App

You'll need to invite your app to your server to interact with it. To do so, navigate to the Installation tab of your app in the Discord Developer Portal.

Run the Bot

You can now run your bot using:

npm run dev

Deploy Your Commands to Discord

Finally, to deploy your commands to Discord, navigate to <BASE_URL>/deploy?secret=<DEPLOY_SECRET> in your browser. This will send your command data to Discord to register them with your bot.

Deploying to Production

Prepare Environment

Before deploying your bot, you'll need to set your environment variables. This can be done using the Wrangler CLI.

npx wrangler secret put DISCORD_PUBLIC_KEY
npx wrangler secret put DISCORD_CLIENT_ID
npx wrangler secret put DISCORD_BOT_TOKEN

Remember to configure your portal URLs to the URL of your Cloudflare Worker.

Deploy to Cloudflare

Once you've set your environment variables, you can deploy your bot with the following command:

npx wrangler deploy

Remember to deploy your commands to Discord using <BASE_URL>/deploy?secret=<DEPLOY_SECRET>.

Last updated on

On this page

Edit on GitHub