Getting started
This guide makes a bot with one /ping command. The bot gets commands over the Discord Gateway, so it runs on your computer with no public URL.
You need Node.js 22.18 or later, which runs TypeScript files, and a Discord account.
Create the Discord application
Section titled “Create the Discord application”-
Open the Discord Developer Portal and create an application.
-
On the General Information page, copy the application ID and the public key.
-
On the Bot page, reset the token and copy it. Keep the token secret: anyone who has it can act as your bot.
-
On the Installation page, add the
applications.commandsandbotscopes to Guild Install. Open the install link and add the bot to a test server. -
Copy the ID of the test server. To see IDs, turn on Developer Mode in the Discord settings, under Advanced. Then right-click the server and select Copy Server ID.
Create the Worker
Section titled “Create the Worker”-
Create a Worker project, then install botflare:
Terminal window npm create cloudflare@latest my-botcd my-botnpm install botflareWhen the tool asks, pick a Hello World Worker in TypeScript.
-
Replace
src/index.tswith this code:import { bot, command } from 'botflare';import { gateway } from 'botflare/gateway';const ping = command({ name: 'ping', description: 'Check that the bot answers' }, (c) =>c.reply('pong'),);const app = bot({ commands: [ping], gateway: 'GATEWAY' });export const Gateway = gateway({ intents: ['Guilds'], bot: app, events: {} });export default app;gateway()makes a Durable Object class. A Durable Object is a Cloudflare object that keeps state and can keep a connection open. This one holds the connection to the Discord Gateway. -
Add the Durable Object to
wrangler.jsonc, next to the settings that are already there:{"durable_objects": { "bindings": [{ "name": "GATEWAY", "class_name": "Gateway" }] },"migrations": [{ "tag": "v1", "new_sqlite_classes": ["Gateway"] }],}The binding name,
GATEWAY, must matchgateway: 'GATEWAY'inbot(). -
Put the values from the Developer Portal in a
.dev.varsfile. Do not commit this file.Terminal window DISCORD_TOKEN=your bot tokenDISCORD_PUBLIC_KEY=your public keyDISCORD_APPLICATION_ID=your application idDISCORD_GUILD_ID=your test server id
Run the bot
Section titled “Run the bot”-
Send the commands to Discord:
Terminal window npx botflare sync src/index.tsbotflare syncreads.dev.vars. WithDISCORD_GUILD_ID, the commands go to your test server only and show at once. -
Start the Worker:
Terminal window npx wrangler dev -
Open the local URL once, such as
http://localhost:8787. The page saysgateway started, and the bot shows as online. -
Type
/pingin your test server. The bot answerspong.
If you change a command name, a description or an option, run npx botflare sync src/index.ts again.
Next steps
Section titled “Next steps”- Deploy the bot to Cloudflare.
- See a bot with options and a button in
examples/basic.