Skip to content

Commit

Permalink
New journey, pre-release
Browse files Browse the repository at this point in the history
  • Loading branch information
en3sis committed Sep 2, 2022
0 parents commit 089b31b
Show file tree
Hide file tree
Showing 70 changed files with 8,800 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.env
.todo
/dist
/k8s/*-secrets.yaml
/k8s/*-kubeconfig.yaml
/src/controllers/_development/*.ts
/node_modules/
/src/config.ts
build/
17 changes: 17 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
ISDEV=true

DISCORD_TOKEN=''
DISCORD_CLIENT_ID=''

#=========== 🍃 MONGO ===========#
MONGODB_CONNECTION="mongodb://hans:S3cret@localhost:27017"
MONGODB_DATABASE='dev'

#=========== 👾 BOT CONFIGURATION ===========#
BOT_GUILD_ID='904719402044383273' # Your Discord Guild ID, the home of your Bot.


# SERVICES, not required but some functionality will be limited
GITHUB_API_TOKEN=""
TIMEZONES_API=''
WEATHER_API=''
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
node_modules/
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": [
"prettier",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": ["@typescript-eslint"],
"rules": {
"quotes": ["error", "single", { "avoidEscape": false, "allowTemplateLiterals": true }],
"no-trailing-spaces": 1,
"@typescript-eslint/explicit-module-boundary-types": 0,
"@typescript-eslint/no-extra-semi": 0,
"@typescript-eslint/no-non-null-assertion": 0
// "eslint@typescript-eslint/no-explicit-any": 0
}
}
31 changes: 31 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)

## Type of change

Please delete options that are not relevant.

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] This change requires a documentation update

# How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

- [ ] Test A
- [ ] Test B

# Checklist:

- [ ] My code follows the style guidelines of this project & I ran `npm run format` before commiting.
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation, if applicable.
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works, if applicable.
- [ ] New and existing unit tests pass locally with my changes
44 changes: 44 additions & 0 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Publish Docker image

on:
# push:
# branches: ['release']
release:
types: [published]

jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v3

- name: Log in to Docker Hub
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: en3sis/hans

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.env
.todo
/dist
/k8s/*-secrets.yaml
/k8s/*-kubeconfig.yaml
/src/controllers/_development/*.ts
!/src/controllers/_development/messageCreate.template.ts
/src/commands/bots-playground/*.ts
/node_modules/
/src/config.ts
build/
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

find . -name ".DS_Store" -delete && npm run lint && npm run format
7 changes: 7 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.gitignore
package-lock.json
package.json
.next
.code
build
9 changes: 9 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"trailingComma": "es5",
"printWidth": 100,
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"bracketSpacing": true
}
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"cSpell.words": ["Cooldown", "ISDEV", "kubectl", "Kubernetes", "MYAPP", "Waifu"]
}
33 changes: 33 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Install dependencies only when needed
FROM node:16.13.0-alpine AS deps
WORKDIR /app

COPY package.json package-lock.json ./
RUN npm instal

# Rebuild the source code only when needed
FROM node:16.13.0-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN npm install
RUN npm run build

# Production image, copy all the files and run next
FROM node:16.13.0-alpine AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 NON_ROOT

COPY --from=builder --chown=NON_ROOT:nodejs /app/build ./build
COPY --from=builder --chown=NON_ROOT:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=NON_ROOT:nodejs /app/package.json ./package.json
COPY --from=builder --chown=NON_ROOT:nodejs /app/package-lock.json ./package-lock.json

USER NON_ROOT

CMD ["npm", "start"]
98 changes: 98 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<div align="center">
<br />
<p>
<a href="https://discord.gg/WpTrnnvJXe"><img src="https://cdn.discordapp.com/attachments/626034007087513601/1014802216831438879/hans-fff.png" width="546" alt="discord.js" /></a>
</p>

</div>

![Docker Pulls](https://img.shields.io/docker/pulls/en3sis/hans?style=for-the-badge)
![Discord](https://img.shields.io/discord/904719402044383273?style=for-the-badge)

# Hans - Discord Bot

Discord Bot build with [Discord.JS](https://discord.js.org/#/), [TypeScript](https://www.typescriptlang.org/), and lots of ❤️

---

> 🪬 **NOTE**: Please consider opening an issue and PR for bugs, suggestions or new features.
---

## 🔅 Prepare environment

Before running any command, run `npm install` & `cp .env.template .env`, fill all the env variables needed. To create your application, visit [Discord's Developer Portal](https://discord.com/developers/docs/intro)

> 🪬 **IMPORTANT**: A MongoDB instance is needed for the bot to work. A free Atlas cluster should be more than enough (even for small bots & communities) for development.
An alternative is to spin a MongoDB instance yourself with Docker locally.
Use `docker compose up -d` command to start it. `docker-compose down` to stop it.

MongoDB Compass is recommended. Your connection string should be `mongodb://hans:S3cret@localhost:27017`

---

## 👩🏼‍💻 Development

### `npm run dev`

Will start a development server with ts-node and nodemon for livereload. A bot Invite link will be displayed in the console. Use it to invite the bot to your server.

### Slash commands

All commands (under `src/commands`) are built with the [Slash Command](https://discordjs.guide/interactions/slash-commands.html) interaction.

> 🪬 **IMPORTANT**: before developing commands, make sure you invite the bot to your server and the entry in mongodb `hans -> config` has the commandsDevGuild.id set to your server id.
All commands under the main folder are available globally (it will take a second to have them available) while the ones under `bots-playground` are guild specific and are instantly deployed, use this folder for debugging & development purposes.

To deploy the commands, run `npm run slash`.

> 🪬 **IMPORTANT**: Since the command process is isolated, make sure to export the mongodb connection string to your current shell, ex: `export MONGODB_CONNECTION="mongodb://hans:S3cret@localhost:27017"`
---

## 🧪 Testing

For testing, we use Mocha with TS.

All the tests are under the `/tests` directory. Right now they're none or a few, ideally, we should add more test coverage for command controllers.

### `npm run test`

Will run all the tests.

---

## 🏗 Production

We have multiple environments for deploying your bot (see Deployment section below).
The easiest way would be to clone the repo and do:

> 🪬 **IMPORTANT**: Make sure to follow the `Prepare environment` section.
### `npm run build`

To generate the application's build.

### `npm start`

Will run the bot with the production environment.

### Other deployments

### With Docker

There's an image in the DockerHub at `en3sis/hans` with the latest version or you can build your image.
Build it locally with `docker build -t en3sis/hans . `

### With Kubernetes

It's also possible to deploy the bot to a Kubernetes cluster, the necessary files are in the `k8s` folder.

**Steps**:

1. You'll need your K8S cluster, ofc ;P
2. Create the namespace `kubectl apply -f k8s/namespace.yaml`
3. Run `cp k8s/bot-secrets.template.yaml k8s/bot-secrets.yaml`, fill it up and apply the secrets `kubectl apply -f k8s/bot-secrets.yaml`
4. Deploy the workload `kubectl apply -f k8s/bot-deployment.yaml`
12 changes: 12 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.9'

services:
mongodb:
image: mongo
ports:
- 27017:27017
volumes:
- ~/apps/mongo:/data/db
environment:
- MONGO_INITDB_ROOT_USERNAME=hans
- MONGO_INITDB_ROOT_PASSWORD=S3cret
57 changes: 57 additions & 0 deletions k8s/bot-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: hans-bot
namespace: bot
spec:
replicas: 1
selector:
matchLabels:
bot: hans
template:
metadata:
labels:
bot: hans
spec:
containers:
- name: hans-container
image: en3sis/hans
resources:
limits:
cpu: 300m
memory: 1000Mi
requests:
cpu: 300m
memory: 1000Mi
imagePullPolicy: IfNotPresent
env:
- name: DISCORD_TOKEN
valueFrom:
secretKeyRef:
name: bot-secrets
key: DISCORD_TOKEN
- name: DISCORD_CLIENT_ID
valueFrom:
secretKeyRef:
name: bot-secrets
key: DISCORD_CLIENT_ID
- name: BOT_GUILD_ID
valueFrom:
secretKeyRef:
name: bot-secrets
key: BOT_GUILD_ID
- name: MONGODB_CONNECTION
valueFrom:
secretKeyRef:
name: bot-secrets
key: MONGODB_CONNECTION
- name: GITHUB_API_TOKEN
valueFrom:
secretKeyRef:
name: bot-secrets
key: GITHUB_API_TOKEN
- name: WEATHER_API
valueFrom:
secretKeyRef:
name: bot-secrets
key: WEATHER_API
6 changes: 6 additions & 0 deletions k8s/bot-namespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apiVersion: v1
kind: Namespace
metadata:
name: bot
labels:
bot: hans
16 changes: 16 additions & 0 deletions k8s/bot-secrets.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Secret
metadata:
name: bot-secrets
namespace: bot
type: Opaque
data: # Fill up with your secrets, use the base64 encoding: echo -n 'YOUR_STRING' | base64
DISCORD_TOKEN: '' # Your Discord bot token
DISCORD_CLIENT_ID: '' # Your Discord client ID
MONGODB_CONNECTION: '' # Your MongoDB connection string, mongodb://hans:S3cret@localhost:27017 if using docker compose for development.
MONGODB_DATABASE: '' # Default database to work with
BOT_GUILD_ID: '' # Your Discord Guild ID, the home of your Bot.

# //INFO: All bellow are optional, but some commands or functionality won't work.
GITHUB_API_TOKEN: '' # Uses to fetch the latest git merge, send the details in a card.
WEATHER_API: '' # Grab one at https://openweathermap.org/api
Loading

0 comments on commit 089b31b

Please sign in to comment.