This repo is an example built on top of the following frameworks
- ReactJS - A frontend javascript UI framework
- Cloudflare Workers - A serverless execution environment
The client (aka front-end) is using ViteJS's React Typescript starter project. Vite has great starter projects in many common javascript frameworks. All of these projects use the same config setup, which means that if you prefer VanillaJS, Svelte, etc... you can swap frameworks and still get the following:
- Fast typescript bundling with hot-module-reloading
- Identical configuration API
- Identical environment variable API
The server (aka back-end) is using Cloudflare workers with typescript. Any file in the server project can be imported by the client, in case you need to share business logic.
Before we write any code, lets follow the instructions here to make sure your Discord application is set up correctly.
As described here, we encourage using a tunnel solution such as cloudflared for local development. To run your app locally, run the following from this directory (/examples/sdk-playground)
pnpm install # only need to run this the first time
pnpm dev
pnpm tunnel # from another terminal
Be sure to complete all the steps listed here to ensure your development setup is working as expected.
In order to add new environment variables, you will need to do the following:
- Add the environment key and value to
.env
- Add the key to /packages/client/src/vite-env.d.ts
- Add the key to /packages/server/src/types.ts
This will ensure that you have type safety in your client and server code when consuming environment variables
Per the ViteJS docs
To prevent accidentally leaking env variables to the client, only variables prefixed with VITE_ are exposed to your Vite-processed code.
# Example .env file
VITE_CLIENT_ID=123456789012345678
CLIENT_SECRET=abcdefghijklmnopqrstuvwxyzabcdef # This should be the application oauth2 token from the developer poortal.
BOT_TOKEN=bot_token
See instructions on getting the oauth2 token and the bot token here.
Steps to manually deploy the embedded app 0. Have access to the Discord Dev cloudflare account
- Log into cloudflare with your credentials associated with Discord Dev
wrangler login
-
Create or verify .env.production file If you haven't made it yet, copy the example.env file, rename it to
.env.production
, and add theVITE_CLIENT_ID
andCLIENT_SECRET
variables -
Build and deploy the client
cd packages/client
npm run build
CLOUDFLARE_ACCOUNT_ID=867c81bb01731ca0dfff534a58ce67d7 npx wrangler pages publish dist
- Build and deploy the server
cd packages/server
npm run deploy