Cloudflare Workers
A guide on how to get started with cloudflare workers and yor.ts
This guide provides a basic outline to set up a Discord bot and integrate it with Cloudflare Workers. Further details and code specifics for each step will be needed based on your specific requirements and programming language preferences.
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.
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
Creating a new project
- Open your favorite code editor
- Create a new folder
- Run
npm init
/yarn init
/pnpm init
to initialize a package.json - Run
npm install yor.ts@latest
/yarn add yor.ts@latest
/pnpm install yor.ts@latest
to install yor.ts
Downloading cloudflare workers cli
-
Run
npm install -g wrangler
/yarn global add wrangler
/pnpm add -g wrangler
to install the cli -
Run
wrangler login
to login to your cloudflare account -
Run
wrangler init
to create a new project -
Put the following in
wrangler.toml
:
Creating src/index.ts
- Add the following to
src/index.ts
:
Esbuild and polyfill
-
Install esbuild and esbuild-plugin-node-polyfill: Run
npm install --save-dev esbuild esbuild-plugin-node-polyfill
oryarn add --dev esbuild esbuild-plugin-node-polyfill
orpnpm add --save-dev esbuild esbuild-plugin-node-polyfill
to 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:
Running the project
- Run
npm run build
oryarn build
orpnpm build
to build the project - Run
wrangler deploy
to deploy your project
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
Testing to see if it works
- Open discord server you invited your bot into
- Run
/ping
Was this page helpful?