Containerize

This commit is contained in:
Benjamin Morgan 2025-12-25 22:48:27 -07:00
parent 71dcefcf01
commit dee736bd54
7 changed files with 59 additions and 8 deletions

4
.gitignore vendored
View file

@ -1,2 +1,4 @@
*.env *.env
*.db *.db
secrets/*
data/*

14
Dockerfile Normal file
View file

@ -0,0 +1,14 @@
FROM python:3.13-slim
WORKDIR /app
COPY docker-entrypoint.sh ./entrypoint.sh
COPY requirements.txt .
COPY schema.sql .
COPY mstbot.py .
RUN apt update && apt install sqlite3
RUN pip install --upgrade pip && pip install -r requirements.txt
ENTRYPOINT [ "/app/entrypoint.sh" ]
CMD [ "python3", "mstbot.py" ]

8
docker-compose.yml Normal file
View file

@ -0,0 +1,8 @@
services:
bot:
image: registry.benrmorgan.com/minecraftstatus:latest
env_file:
- ./secrets/.env
volumes:
- ./data:/data
restart: unless-stopped

15
docker-entrypoint.sh Executable file
View file

@ -0,0 +1,15 @@
#!/bin/sh
set -e
DB_PATH="/data/mstbot.db"
SCHEMA="/app/schema.sql"
if [ ! -f "$DB_PATH" ]; then
echo "SQLite DB not found, initializing…"
mkdir -p "$(dirname "$DB_PATH")"
sqlite3 "$DB_PATH" < "$SCHEMA"
else
echo "SQLite DB exists, skipping init"
fi
exec "$@"

View file

@ -1,6 +1,4 @@
# TODO: Change hours column to boolean, drop times table, make it a column in servers # TODO: Change hours column to boolean, drop times table, make it a column in servers
# TODO: Containerize, include sql script to initialize empty database if one does not exist in volume
# TODO: CICD deploy over live instance via compose
# TODO: prevent sql injection # TODO: prevent sql injection
# TODO: annotate return types everywhere # TODO: annotate return types everywhere
# TODO: split cogs into separate files, add utils file, remove globals # TODO: split cogs into separate files, add utils file, remove globals
@ -21,12 +19,8 @@ from typing import Union, List
import sqlite3 import sqlite3
import discord import discord
from discord.ext import commands from discord.ext import commands
from dotenv import load_dotenv
from mcstatus import JavaServer from mcstatus import JavaServer
# Load environment variables
load_dotenv('.env')
# Bot Initialization # Bot Initialization
intents = discord.Intents.default() intents = discord.Intents.default()
intents.messages = True intents.messages = True
@ -356,7 +350,7 @@ async def getStatus(serv: JavaServer):
def connect(): def connect():
mydb = sqlite3.connect('mstbot.db') mydb = sqlite3.connect('/data/mstbot.db')
cursor = mydb.cursor() cursor = mydb.cursor()
return mydb, cursor return mydb, cursor

15
requirements.txt Normal file
View file

@ -0,0 +1,15 @@
aiohappyeyeballs==2.6.1
aiohttp==3.13.2
aiosignal==1.4.0
asyncio-dgram==2.2.0
attrs==25.4.0
audioop-lts==0.2.2
discord.py==2.6.4
dnspython==2.8.0
frozenlist==1.8.0
idna==3.11
mcstatus==12.0.6
multidict==6.7.0
propcache==0.4.1
setuptools==80.9.0
yarl==1.22.0

3
schema.sql Normal file
View file

@ -0,0 +1,3 @@
CREATE TABLE names (id INTEGER NOT NULL, name TEXT NOT NULL, UNIQUE (id, name));
CREATE TABLE times (id INTEGER NOT NULL PRIMARY KEY, time NOT NULL DEFAULT 0) WITHOUT ROWID;
CREATE TABLE servers (id INTEGER PRIMARY KEY, ip TEXT NOT NULL, port INTEGER NOT NULL DEFAULT 25565, last_query TIMESTAMP, hours REAL NOT NULL DEFAULT 0, announce_joins BOOLEAN NOT NULL DEFAULT FALSE, announce_joins_id INTEGER) WITHOUT ROWID;