Deploy
A bot gets interactions from Discord in one of two ways. Pick one before you deploy.
| Way | How Discord reaches the bot | Gets gateway events |
|---|---|---|
| Gateway | A Durable Object keeps a WebSocket to the Discord Gateway open. | Yes |
| HTTP | Discord sends a POST request to the Worker URL, the Interactions Endpoint URL. | No |
The gateway keeps one Durable Object running all day. With 128 MB of memory, that uses about 11,000 GB-s of duration per day, or about 330,000 GB-s per month. The Durable Objects pricing includes 13,000 GB-s per day on Workers Free and 400,000 GB-s per month on Workers Paid. Either plan covers one bot, but not two. The HTTP way uses no Durable Object time.
Deploy over the gateway
Section titled “Deploy over the gateway”The Worker from Getting started uses the gateway.
-
Deploy the Worker:
Terminal window npx wrangler deploy -
Add the bot token as a secret:
Terminal window npx wrangler secret put DISCORD_TOKEN -
Open the Worker URL once, such as
https://my-bot.your-account.workers.dev. The page saysgateway started.After that, a Durable Object alarm checks the connection every minute. If the connection is down, the alarm opens it again. A new deploy restarts the Durable Object, and the alarm connects it again within a minute.
Deploy over HTTP
Section titled “Deploy over HTTP”-
Remove
gateway: 'GATEWAY'and theGatewayexport fromsrc/index.ts, and remove the Durable Object fromwrangler.jsonc. -
Deploy the Worker and add the public key as a secret:
Terminal window npx wrangler deploynpx wrangler secret put DISCORD_PUBLIC_KEYIf the bot calls
rest, sends messages or runs cron jobs, also addDISCORD_TOKEN. -
In the Developer Portal, on the General Information page, set the Interactions Endpoint URL to the Worker URL. Discord sends a PING request to check the URL.
bot()checks its signature and answers it.
Send the commands to every server
Section titled “Send the commands to every server”botflare sync sends the commands to the server in DISCORD_GUILD_ID. To send them to every server that has the bot, remove DISCORD_GUILD_ID and run the sync again:
npx botflare sync src/index.tsEnvironment variables
Section titled “Environment variables”| Name | Used by |
|---|---|
DISCORD_TOKEN |
The gateway, rest, c.send and botflare sync. |
DISCORD_PUBLIC_KEY |
bot(), to check requests that come over HTTP. |
DISCORD_APPLICATION_ID |
botflare sync. |
DISCORD_GUILD_ID |
botflare sync. If you set it, the commands go to that server only. |