Skip to content

Commit

Permalink
Merge pull request #116 from satisfactorymodding/dev
Browse files Browse the repository at this point in the history
v2.21.0
  • Loading branch information
Feyko authored Aug 14, 2024
2 parents 756e1e2 + df16082 commit b2e231d
Show file tree
Hide file tree
Showing 39 changed files with 719 additions and 490 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Auto detect text files and perform LF normalization
* text=auto

# Keep shell files as LF so Windows-host-machine users can still use Docker
*.sh text eol=lf
30 changes: 11 additions & 19 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ on:
tags: [ 'v*.*.*' ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: feyko/ficsit-fred
IMAGE_NAME: ${{ github.repository }}


jobs:
Expand All @@ -26,31 +24,25 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2

# Login against a Docker registry except on PR
uses: actions/checkout@v4

- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: feyko
password: ${{ secrets.GH_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- uses: docker/setup-buildx-action@v1
id: buildx

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- uses: docker/setup-buildx-action@v3

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
file: ./docker/Dockerfile
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ celerybeat.pid

# Environments
.env
!example.env
.venv
.run/
env/
Expand Down
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
# FICSIT-Fred

A Discord bot for the Satisfactory Modding Discord

Thanks to Illya, creator of the bot !
## Development

Want to contribute to Fred? Here's everything you need to know to get started.

### Prerequisites

#### A Discord token

First of all, you'll need a Discord bot token for Fred to connect to Discord with.
To get one, you can go to <https://discord.com/developers/applications>, register a new application, add a bot to the application and **activate the "message content intent"**.
This intent is needed to receive the content of messages.
Fred doesn't use slash commands so this is simply needed for him to function.

#### Docker

Docker is for making containers. If you're not sure what that means, look it up it's real cool! In our case, it helps use set up all needed dependencies for Fred without you needing to do anything other than install Docker.
You can get docker [here](https://docs.docker.com/engine/install/). For Linux, make sure to **not** use Docker Desktop. For Windows, it is the easiest way.
If you don't want to install Docker (especially on Windows where Docker Desktop can take up resources and requires virtualisation to be enabled), you can also manually set up a PostgreSQL DB and configure Fred to point to it. More on that later.

#### (Optional) Dialogflow auth

This is optional because this feature is currently disabled in Fred.
You'll have get authentication information for dialogflow if you want to work on that.

### Setup

Two choices here: All through docker or hybrid local/docker.
I recommend the hybrid way for ease of development. We don't have a proper devcontainer setup so debugging Fred when running in Docker is not great.
Instead, in a hybrid setup, you'll use Docker for the postgres DB only and run Fred locally using Poetry.

#### Docker-only

Run `FRED_TOKEN=<your_token> docker compose up -d` and that's it! Fred should run.
(Note: This command assumes you're using bash as your shell. For Powershell or other, set the environment variable differently).

You can verify that the database was properly created and manage it by going to <http://localhost:8080> where pgadmin should show.
You can use `[email protected]` for the user and `fred` for the password. All of this is customizable in `docker-compose.yml`.

#### Hybrid

For this, you'll need [poetry](https://python-poetry.org/) installed, as well as `libpq` for the connection to the postgres DB.
For libpq, you can install `libpq-dev` (or equivalent) on linux and [install postgres](https://www.postgresql.org/download/windows/) on Windows (test without it first actually I'm not certain this is needed).

Once Poetry is installed, run `poetry install`. This should create a `.venv` folder that contains the virtualenv with all the necessary packages. Activate it using `poetry shell` or manually.

Now, run `docker compose up -d -f docker-compose-deps.yml`. This should spin up the postgres DB.
You can verify that the database was properly created and manage it by going to <http://localhost:8080> where pgadmin should show.
You can use `[email protected]` for the user and `fred` for the password. All of this is customizable in `docker-compose-deps.yml`.

Almost there! You'll now have to configure Fred. This just means setting the following env vars (found in `fred/__main__.py`).

```sh
"FRED_IP",
"FRED_PORT",
"FRED_TOKEN",
"FRED_SQL_DB",
"FRED_SQL_USER",
"FRED_SQL_PASSWORD",
"FRED_SQL_HOST",
"FRED_SQL_PORT",
```

For convenience, a `.env` file is included and Fred will load them automatically. It includes defaults that will work with the config of the docker-compose-deps.yml. You'll only have to set `FRED_TOKEN` and maybe change the others if the defaults don't suit you.

Finally, run `python fred/__main__.py`. Fred should run! You can now adapt this to your setup and run the script from your IDE instead. Don't forget to use the virtualenv python!

## Thanks

Massive thanks to Borketh, Mircea and everyone else that has contributed!
19 changes: 19 additions & 0 deletions cspell.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// https://cspell.org/configuration/
{
// Version of the setting file. Always 0.2
"version": "0.2",
// language - current active spelling language
"language": "en",
// words - list of words to be always considered correct
"words": [
"FICSIT",
"libpq",
"Mircea",
"pgadmin",
"virtualenv"
],
// flagWords - list of words to be always considered incorrect
// This is useful for offensive words and common spelling errors.
// cSpell:disable (don't complain about the words we listed here)
"flagWords": []
}
25 changes: 25 additions & 0 deletions docker-compose-deps.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
services:
postgres:
image: postgres:14.0-alpine
environment:
POSTGRES_PASSWORD: fred
POSTGRES_USER: fred
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
volumes:
- pgdata:/var/lib/postgresql/data/pgdata:z
- ./docker/exampledb.sql:/docker-entrypoint-initdb.d/exampledb.sql

pgadmin:
depends_on:
- postgres
image: dpage/pgadmin4:6
environment:
PGADMIN_DEFAULT_EMAIL: '[email protected]'
PGADMIN_DEFAULT_PASSWORD: 'fred'
ports:
- "8080:80"

volumes:
pgdata:
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3'
services:
fred:
depends_on:
Expand Down
29 changes: 17 additions & 12 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
FROM python:3.10-slim as runtime
FROM python:3.12-alpine AS runtime
#FROM python:3.12-slim AS runtime

ENV DEBIAN_FRONTEND=noninteractive
VOLUME /config
WORKDIR /app

COPY docker/runtime-deps.sh .
RUN bash runtime-deps.sh 1> /dev/null && rm runtime-deps.sh
RUN apk update; apk add --no-cache tesseract-ocr-data-eng
#ENV DEBIAN_FRONTEND=noninteractive APT="apt-get -qq"
#RUN $APT update; \
# $APT install tesseract-ocr; \
# $APT clean; \
# rm -rf /var/lib/apt/lists/*

FROM python:3.10-slim as build
FROM python:3.12-alpine AS build
#FROM python:3.12-slim AS build

WORKDIR /app

RUN apt-get -qq update && apt-get -qq install curl libpq-dev gcc 1> /dev/null
WORKDIR /deps

RUN curl -sSL https://install.python-poetry.org | POETRY_HOME=/usr/local python3 -
RUN pip --no-cache-dir install --progress-bar=off "poetry==1.8"

COPY pyproject.toml .
RUN poetry install -nvvv --no-dev && mv $(poetry env info --path) /app/venv
COPY poetry.lock .
RUN poetry install -nvvv --only main --no-root; mv $(poetry env info --path) ./venv

FROM runtime

COPY --from=build /app/venv /app/venv
COPY fred *.env ./fred/
COPY --from=build /deps/venv ./venv
COPY fred ./fred/
COPY *.env .

CMD ./venv/bin/python3 -m fred
48 changes: 0 additions & 48 deletions docker/runtime-deps.sh

This file was deleted.

8 changes: 8 additions & 0 deletions example.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FRED_IP=::
FRED_PORT=80
FRED_SQL_DB=fred
FRED_SQL_USER=fred
FRED_SQL_PASSWORD=fred
FRED_SQL_HOST=0.0.0.0
FRED_SQL_PORT=5432
FRED_LOG_LEVEL=DEBUG
30 changes: 29 additions & 1 deletion fred/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@
from .fred import __version__
import logging
from os import getenv

from dotenv import load_dotenv

logging.root = logging.getLogger("FRED")
logging.basicConfig(level=getenv("FRED_LOG_LEVEL", logging.DEBUG))

from .fred import __version__ # noqa

logging.root.debug(f"HELLO WORLD!!! FRED version: {__version__}")

ENVVARS = (
"FRED_IP",
"FRED_PORT",
"FRED_TOKEN",
"FRED_SQL_DB",
"FRED_SQL_USER",
"FRED_SQL_PASSWORD",
"FRED_SQL_HOST",
"FRED_SQL_PORT",
# "DIALOGFLOW_AUTH",
)

load_dotenv()

for var in ENVVARS:
if getenv(var) is None:
raise EnvironmentError(f"The ENV variable '{var}' isn't set")
22 changes: 1 addition & 21 deletions fred/__main__.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
from dotenv import load_dotenv
from os import getenv

load_dotenv()

ENVVARS = [
"FRED_IP",
"FRED_PORT",
"FRED_TOKEN",
"FRED_SQL_DB",
"FRED_SQL_USER",
"FRED_SQL_PASSWORD",
"FRED_SQL_HOST",
"FRED_SQL_PORT",
"DIALOGFLOW_AUTH",
]

for var in ENVVARS:
if getenv(var) is None:
raise EnvironmentError(f"The ENV variable '{var}' isn't set")

from .fred import Bot, nextcord, __version__

from .fred import Bot, nextcord

intents = nextcord.Intents.all()

Expand Down
Loading

0 comments on commit b2e231d

Please sign in to comment.