{"id":15041870,"url":"https://github.com/discord/cloudflare-sample-app","last_synced_at":"2025-04-08T09:08:57.661Z","repository":{"id":38212048,"uuid":"487125220","full_name":"discord/cloudflare-sample-app","owner":"discord","description":"Example discord bot using Cloudflare Workers","archived":false,"fork":false,"pushed_at":"2025-03-31T05:23:22.000Z","size":475,"stargazers_count":348,"open_issues_count":9,"forks_count":106,"subscribers_count":17,"default_branch":"main","last_synced_at":"2025-04-01T08:37:29.936Z","etag":null,"topics":["samples"],"latest_commit_sha":null,"homepage":"https://discord.com/developers/docs/tutorials/hosting-on-cloudflare-workers","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/discord.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-29T22:26:20.000Z","updated_at":"2025-03-31T08:12:45.000Z","dependencies_parsed_at":"2023-11-07T06:25:43.861Z","dependency_job_id":"4447283f-6b29-4929-a11e-32ba1e8cdd44","html_url":"https://github.com/discord/cloudflare-sample-app","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/discord%2Fcloudflare-sample-app","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/discord%2Fcloudflare-sample-app/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/discord%2Fcloudflare-sample-app/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/discord%2Fcloudflare-sample-app/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/discord","download_url":"https://codeload.github.com/discord/cloudflare-sample-app/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247809962,"owners_count":20999816,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["samples"],"created_at":"2024-09-24T20:46:34.957Z","updated_at":"2025-04-08T09:08:57.605Z","avatar_url":"https://github.com/discord.png","language":"JavaScript","readme":"# Cloudflare worker example app\n\nawwbot is an example app that brings the cuteness of `r/aww` straight to your Discord server, hosted on Cloudflare workers. Cloudflare Workers are a convenient way to host Discord bots due to the free tier, simple development model, and automatically managed environment (no VMs!).\n\nThe tutorial for building awwbot is [in the developer documentation](https://discord.com/developers/docs/tutorials/hosting-on-cloudflare-workers)\n\n![awwbot in action](https://user-images.githubusercontent.com/534619/157503404-a6c79d1b-f0d0-40c2-93cb-164f9df7c138.gif)\n\n## Resources used\n\n- [Discord Interactions API](https://discord.com/developers/docs/interactions/receiving-and-responding)\n- [Cloudflare Workers](https://workers.cloudflare.com/) for hosting\n- [Reddit API](https://www.reddit.com/dev/api/) to send messages back to the user\n\n---\n\n## Project structure\n\nBelow is a basic overview of the project structure:\n\n```\n├── .github/workflows/ci.yaml -\u003e Github Action configuration\n├── src\n│   ├── commands.js           -\u003e JSON payloads for commands\n│   ├── reddit.js             -\u003e Interactions with the Reddit API\n│   ├── register.js           -\u003e Sets up commands with the Discord API\n│   ├── server.js             -\u003e Discord app logic and routing\n├── test\n|   ├── test.js               -\u003e Tests for app\n├── wrangler.toml             -\u003e Configuration for Cloudflare workers\n├── package.json\n├── README.md\n├── .eslintrc.json\n├── .prettierignore\n├── .prettierrc.json\n└── .gitignore\n```\n\n## Configuring project\n\nBefore starting, you'll need a [Discord app](https://discord.com/developers/applications) with the following permissions:\n\n- `bot` with the `Send Messages` and `Use Slash Command` permissions\n- `applications.commands` scope\n\n\u003e ⚙️ Permissions can be configured by clicking on the `OAuth2` tab and using the `URL Generator`. After a URL is generated, you can install the app by pasting that URL into your browser and following the installation flow.\n\n## Creating your Cloudflare worker\n\nNext, you'll need to create a Cloudflare Worker.\n\n- Visit the [Cloudflare dashboard](https://dash.cloudflare.com/)\n- Click on the `Workers` tab, and create a new service using the same name as your Discord bot\n\n## Running locally\n\nFirst clone the project:\n\n```\ngit clone https://github.com/discord/cloudflare-sample-app.git\n```\n\nThen navigate to its directory and install dependencies:\n\n```\ncd cloudflare-sample-app\nnpm install\n```\n\n\u003e ⚙️ The dependencies in this project require at least v18 of [Node.js](https://nodejs.org/en/)\n\n### Local configuration\n\n\u003e 💡 More information about generating and fetching credentials can be found [in the tutorial](https://discord.com/developers/docs/tutorials/hosting-on-cloudflare-workers#storing-secrets)\n\nRename `example.dev.vars` to `.dev.vars`, and make sure to set each variable.\n\n**`.dev.vars` contains sensitive data so make sure it does not get checked into git**.\n\n### Register commands\n\nThe following command only needs to be run once:\n\n```\n$ npm run register\n```\n\n### Run app\n\nNow you should be ready to start your server:\n\n```\n$ npm start\n```\n\n### Setting up ngrok\n\nWhen a user types a slash command, Discord will send an HTTP request to a given endpoint. During local development this can be a little challenging, so we're going to use a tool called `ngrok` to create an HTTP tunnel.\n\n```\n$ npm run ngrok\n```\n\n![forwarding](https://user-images.githubusercontent.com/534619/157511497-19c8cef7-c349-40ec-a9d3-4bc0147909b0.png)\n\nThis is going to bounce requests off of an external endpoint, and forward them to your machine. Copy the HTTPS link provided by the tool. It should look something like `https://8098-24-22-245-250.ngrok.io`. Now head back to the Discord Developer Dashboard, and update the \"Interactions Endpoint URL\" for your bot:\n\n![interactions-endpoint](https://user-images.githubusercontent.com/534619/157510959-6cf0327a-052a-432c-855b-c662824f15ce.png)\n\nThis is the process we'll use for local testing and development. When you've published your bot to Cloudflare, you will _want to update this field to use your Cloudflare Worker URL._\n\n## Deploying app\n\nThis repository is set up to automatically deploy to Cloudflare Workers when new changes land on the `main` branch. To deploy manually, run `npm run publish`, which uses the `wrangler publish` command under the hood. Publishing via a GitHub Action requires obtaining an [API Token and your Account ID from Cloudflare](https://developers.cloudflare.com/workers/wrangler/cli-wrangler/authentication/#generate-tokens). These are stored [as secrets in the GitHub repository](https://docs.github.com/en/actions/security-guides/encrypted-secrets#creating-encrypted-secrets-for-a-repository), making them available to GitHub Actions. The following configuration in `.github/workflows/ci.yaml` demonstrates how to tie it all together:\n\n```yaml\nrelease:\n  if: github.ref == 'refs/heads/main'\n  runs-on: ubuntu-latest\n  needs: [test, lint]\n  steps:\n    - uses: actions/checkout@v3\n    - uses: actions/setup-node@v3\n      with:\n        node-version: 18\n    - run: npm install\n    - run: npm run publish\n      env:\n        CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}\n        CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}\n```\n\n### Storing secrets\n\nThe credentials in `.dev.vars` are only applied locally. The production service needs access to credentials from your app:\n\n```\n$ wrangler secret put DISCORD_TOKEN\n$ wrangler secret put DISCORD_PUBLIC_KEY\n$ wrangler secret put DISCORD_APPLICATION_ID\n```\n\n## Questions?\n\nFeel free to post an issue here, or reach out to [@justinbeckwith](https://twitter.com/JustinBeckwith)!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiscord%2Fcloudflare-sample-app","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdiscord%2Fcloudflare-sample-app","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdiscord%2Fcloudflare-sample-app/lists"}