Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
radityaharya committed Feb 17, 2024
0 parents commit 5984423
Show file tree
Hide file tree
Showing 24 changed files with 8,653 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
coverage
.dev.vars
.env
27 changes: 27 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"extends": [
"prettier",
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": [
"prettier",
"@typescript-eslint"
],
"rules": {
"prettier/prettier": ["error"]
},
"env": {
"serviceworker": true
},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": "latest",
"project": "./tsconfig.json"
},
"ignorePatterns": [
"node_modules",
"**/*.js" // Ignore all .js files
]
}
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
ignore:
- dependency-name: "*"
update-types:
["version-update:semver-patch", "version-update:semver-minor"]
39 changes: 39 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
on:
push:
branches:
- main
pull_request:
name: ci
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: npm test
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: npm run lint
release:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
needs: [test, lint]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
- run: npm install
- run: npm run publish
env:
CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
coverage
.dev.vars
.env
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
3 changes: 3 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM oven/bun:latest
WORKDIR /app
COPY package.json .
RUN bun install
COPY . .
EXPOSE 3000
ENV PORT=3000
ENTRYPOINT ["bun", "docker-entrypoint.js"]
86 changes: 86 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Ryo

Ryo is a Discord bot that uses the [Discord Interactions API](https://discord.com/developers/docs/interactions/receiving-and-responding) to respond to slash commands. It's built using [Hono](https://hono.dev/) for [Cloudflare Workers](https://workers.cloudflare.com/), which allows you to run serverless functions at the edge of Cloudflare's network.

## Features
- [x] Slash commands
- [x] Reddit Hot Posts from a Subreddit
- [x] Forwards Email to Discord using Cloudflare Email Routing


## Configuring project

Before starting, you'll need a [Discord app](https://discord.com/developers/applications) with the following permissions:

- `bot` with the `Send Messages` and `Use Slash Command` permissions
- `applications.commands` scope

> ⚙️ 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.
## Email to Discord

This bot also has the ability to forward emails to a Discord channel. The worker will be registered as an Email Routing rule in Cloudflare. When an email is sent to the custom email address, it will be forwarded to the Discord channel using the Webhook URL (how to get Webhook URL [[Doc](https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks)])

**Prerequisites:**
1. Have email routing enabled on a cloudflare Website [[Doc](https://developers.cloudflare.com/email-routing/get-started/enable-email-routing/)]
2. Authenticated Wrangler CLI setup [[Doc](https://developers.cloudflare.com/workers/wrangler)]

**Setup:**
1. Adding Webhook URL as Secret
- Using Wrangler CLI
```bash
wrangler secret put DISCORD_WEBHOOK_URL
```
Paste Webhook URL
```bash
? Enter a secret value: ›
```
2. Setting Worker as a rule for custom address
1. In Cloudflare dashboard go to Email > Email Routing > Routes.
2. Select Create address.
3. In Custom address, enter the custom email address you want to use (for example, 'ryo').
4. In the Action drop-down menu, choose "send to worker"
5. Select "ryo" as the destination

## Deploying

```bash
git clone https://github.com/radityaharya/ryo.git
cd ryo
```

To install the dependencies, run the following command:

```bash
npm install
```

Set up your environment variables by creating a `.env` file in the root of the project. You can use the `example.env` file as a template.

To deploy the app, you'll need to install the [Wrangler CLI](https://developers.cloudflare.com/workers/cli-wrangler/install-update). Once installed, you can deploy the app using the following command:
```bash
wrangler deploy
```
The credentials in `.env` are only applied locally. The production service needs access to credentials from your app:
```bash
wrangler secret put DISCORD_TOKEN
wrangler secret put DISCORD_PUBLIC_KEY
wrangler secret put DISCORD_APPLICATION_ID
wrangler secret put DISCORD_WEBHOOK_URL
wrangler secret put BOT_SECRET
```
## Setting up the bot
After deploying the app, you'll need to register the bot with Discord. You can do this by inputting your `interactions` URL into the `Interactions Endpoint URL` field in the `General Information` tab of the Discord Developer Portal.

**Registering Commands**

Commands should be registered with Discord using the `register` command after the bot has been deployed or when commands are updated.

```bash
npm run register
```
Binary file added bun.lockb
Binary file not shown.
38 changes: 38 additions & 0 deletions docker-entrypoint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { app } from './src/server.ts';
import { env } from 'hono/adapter';

const {
PORT = 3000,
HOST = '0.0.0.0',
DISCORD_APPLICATION_ID,
DISCORD_PUBLIC_KEY,
DISCORD_TOKEN,
BOT_SECRET,
} = env();

if (!DISCORD_APPLICATION_ID) {
throw new Error('DISCORD_APPLICATION_ID is required');
}

if (!DISCORD_PUBLIC_KEY) {
throw new Error('DISCORD_PUBLIC_KEY is required');
}

if (!DISCORD_TOKEN) {
throw new Error('DISCORD_TOKEN is required');
}

if (!BOT_SECRET) {
throw new Error('BOT_SECRET is required');
}


console.log('Starting server...');
console.log('Server is running on port ' + PORT);
console.log('App ID: ', DISCORD_APPLICATION_ID);

export default {
host: HOST,
port: 3000,
fetch: app.fetch,
};
6 changes: 6 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
DISCORD_APPLICATION_ID=".."
DISCORD_PUBLIC_KEY=".."
DISCORD_TOKEN=".."
DISCORD_WEBHOOK_URL=".." # for email forwarding
BOT_SECRET=".." # random string for bot authentication
BOT_URL="<your bot url>.workers.dev" # your bot url
Loading

0 comments on commit 5984423

Please sign in to comment.