countdown-bot

A Discord bot that runs countdown games and generates analytics
git clone https://git.ashermorgan.net/countdown-bot/
Log | Files | Refs | README

commit 243ae48f610b0ea961b446c737a33d6dce6fd6c6
parent 7ea6876043b258afcdd32b0777aadbd4e720ff94
Author: Asher Morgan <59518073+ashermorgan@users.noreply.github.com>
Date:   Wed, 12 Jun 2024 14:17:21 -0700

Dockerize the bot

Diffstat:
MREADME.md | 48++++++++++++++++++++++++++++++++++--------------
Abot.Dockerfile | 13+++++++++++++
Acompose.yml | 30++++++++++++++++++++++++++++++
Mcountdown_bot/__main__.py | 2+-
Adb.Dockerfile | 6++++++
Mrequirements.txt | 2+-
6 files changed, 85 insertions(+), 16 deletions(-)

diff --git a/README.md b/README.md @@ -1,42 +1,62 @@ # countdown-bot -A Discord bot that facilitates countdowns and generates detailed countdown analytics +A Discord bot that facilitates countdowns and generates detailed countdown +analytics +## Setup +1. Go to the [Discord Developer Portal](https://discord.com/developers/) and + create an application and a bot +2. Setup a database and run the bot (see below) -## Setup +3. Add the bot to your server: + `https://discordapp.com/oauth2/authorize?client_id=BOT_ID_HERE&scope=bot&permissions=101440` + +4. Send `!help` to the bot to get instructions for getting started + +### Running with Docker +Create the `.env` file and add settings: + +``` +BOT_TOKEN=... +DB_PASSWORD=... +``` + +Start docker containers: + +``` +docker compose up +``` + +### Running for development Install the Python dependencies + ``` pip install -r requirements.txt ``` -Go to the [Discord Developer Portal](https://discord.com/developers/) and create an application and a bot - Create `.env` file and add settings: + ``` -TOKEN=... +BOT_TOKEN=... PREFIX=! DATABASE=postgresql://... LOG_FILE=log.txt LOG_LEVEL=INFO ``` -Initialize the PostgreSQL database +Setup a PostgreSQL database and initialize it: + ``` -psql 'postgresql://...' -f models/ddl.sql -f models/dml-utils.sql -f models/dml-core.sql -f models/dml-analytics.sql +psql 'postgresql://...' -f models/ddl.sql -f models/dml-utils.sql \ + -f models/dml-core.sql -f models/dml-analytics.sql ``` Run the bot -``` -python -m countdown_bot -``` -Add the bot to your server ``` -https://discordapp.com/oauth2/authorize?client_id=BOT_ID_HERE&scope=bot&permissions=101440 +python -m countdown_bot ``` -Send `!help` to the bot get a list of commands and a description of the bot's behavior - ## Screenshots ![Help information](screenshots/help.png) diff --git a/bot.Dockerfile b/bot.Dockerfile @@ -0,0 +1,13 @@ +FROM python:3-alpine + +RUN apk update && apk add gcc musl-dev postgresql-dev python3-dev + +WORKDIR /app + +COPY requirements.txt . +RUN python3 -m pip install --no-cache-dir -r requirements.txt + +COPY countdown_bot . + +WORKDIR / +CMD ["python", "-m", "app"] diff --git a/compose.yml b/compose.yml @@ -0,0 +1,30 @@ +name: countdown-bot + +services: + bot: + build: + context: . + dockerfile: bot.Dockerfile + environment: + - BOT_TOKEN=${BOT_TOKEN} + - DATABASE=postgresql://postgres:${DB_PASSWORD}@db:5432 + depends_on: + db: + condition: service_healthy + + db: + build: + context: . + dockerfile: db.Dockerfile + environment: + - POSTGRES_PASSWORD=${DB_PASSWORD} + volumes: + - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 1s + timeout: 5s + retries: 10 + +volumes: + pgdata: diff --git a/countdown_bot/__main__.py b/countdown_bot/__main__.py @@ -24,4 +24,4 @@ db_connection = psycopg.connect(os.environ.get("DATABASE"), row_factory=psycopg. # Run bot bot = CountdownBot(db_connection, os.environ.get("PREFIX", "!")) -bot.run(os.environ.get("TOKEN")) +bot.run(os.environ.get("BOT_TOKEN")) diff --git a/db.Dockerfile b/db.Dockerfile @@ -0,0 +1,6 @@ +FROM postgres:alpine + +ADD models/ddl.sql /docker-entrypoint-initdb.d +ADD models/dml-analytics.sql /docker-entrypoint-initdb.d +ADD models/dml-core.sql /docker-entrypoint-initdb.d +ADD models/dml-utils.sql /docker-entrypoint-initdb.d diff --git a/requirements.txt b/requirements.txt @@ -1,4 +1,4 @@ discord matplotlib -psycopg2 +psycopg python-dotenv