This guide assumes you are having a cloudflare account
We are going to use typescript in this guide
1
Creating Discord Application
Creating a Discord Bot and Obtaining Credentials
- Go to the Discord Developer Portal.
- Click on “New Application” and give it a name.
- Navigate to the “Bot” tab on the left sidebar.
- Click “Add Bot” and confirm.
- Note down the Client ID under the “APP DETAILS” section.
- Click “Copy” under the “TOKEN” section to get the bot token.
2
Inviting Bot to Server
- Still in the Developer Portal, go to the “OAuth2” tab.
- Under “OAuth2 URL Generator,” select “bot” and “application commands” in scopes.
- Copy the generated URL and paste it into your browser.
- Authorize the bot to a server you have access to
3
Creating a new project
- Open your favorite code editor
- Create a new folder
- Run
npm init/yarn init/pnpm initto initialize a package.json - Run
npm install yor.ts@latest/yarn add yor.ts@latest/pnpm install yor.ts@latestto install yor.ts
4
Downloading cloudflare workers cli
-
Run
npm install -g wrangler/yarn global add wrangler/pnpm add -g wranglerto install the cli -
Run
wrangler loginto login to your cloudflare account -
Run
wrangler initto create a new project -
Put the following in
wrangler.toml:
5
Creating src/index.ts
- Add the following to
src/index.ts:
6
Esbuild and polyfill
-
Install esbuild and esbuild-plugin-node-polyfill: Run
npm install --save-dev esbuild esbuild-plugin-node-polyfilloryarn add --dev esbuild esbuild-plugin-node-polyfillorpnpm add --save-dev esbuild esbuild-plugin-node-polyfillto install these packages as dev dependencies. - Update the package.json file: Add the following scripts to the scripts section of your package.json file:
- Create build.js.
- Add the following to the build.js config file:
7
Running the project
- Run
npm run buildoryarn buildorpnpm buildto build the project - Run
wrangler deployto deploy your project
8
Testing the bot
- Pick the URL from the wrangler logs
- Open the URL in your browser
- Go to the discord developer portal and add the URL and submit it as interactions endpoint
9
Testing to see if it works
- Open discord server you invited your bot into
- Run
/ping
