A simple Discord bot that responds to /ping commands with "pong". Built with Cloudflare Workers.
- Node.js v16+
- Discord Application with Bot Token
- Cloudflare Workers account
- ngrok account (for local development)
- Go to the Discord Developer Portal
- Create a new application or select an existing one
- Go to the "Bot" section and create a bot
- Copy the Bot Token (this is your
DISCORD_TOKEN) - Go to "General Information" and copy the Application ID (this is your
DISCORD_APPLICATION_ID) - In the "Bot" section, ensure the bot has the
applications.commandsscope
- Sign up for a free account at ngrok.com
- Install ngrok:
- Linux/macOS:
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null && echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list && sudo apt update && sudo apt install ngrok - Or download directly: Download from ngrok downloads
- Linux/macOS:
- Connect your account:
ngrok config add-authtoken <your_authtoken>- Get your authtoken from the ngrok dashboard
- Install dependencies:
npm install- Set environment variables for local development:
export DISCORD_TOKEN=your_bot_token
export DISCORD_APPLICATION_ID=your_application_id- Add secrets to Cloudflare Workers:
npx wrangler secret put DISCORD_TOKEN
npx wrangler secret put DISCORD_APPLICATION_ID
npx wrangler secret put DISCORD_PUBLIC_KEY- Register the slash command:
node src/register.jsStart local development server:
npm run devFor local testing with Discord, use ngrok to expose your local server:
npm run ngrokUpdate your Discord application's Interactions Endpoint URL with the ngrok HTTPS URL.
Deploy to Cloudflare Workers:
npm run deployUpdate your Discord application's Interactions Endpoint URL with your Cloudflare Worker URL.
/ping- Bot responds with "pong"
src/commands.js- Command definitionssrc/server.js- Main bot logic and request handlingsrc/register.js- Command registration script