This commit is contained in:
Franz Rolfsvaag 2025-08-09 16:20:13 +02:00
parent 6381c0eb58
commit a11de2770f
5 changed files with 70 additions and 31 deletions

14
bot.py
View File

@ -11,9 +11,14 @@ import pathlib
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
# Load settings (we won't overwrite your file)
# Where to read settings from
CONFIG_PATH = os.getenv('SHAI_CONFIG', '/config/settings.conf')
# Load settings
config = ConfigParser()
config.read('settings.conf')
read_ok = config.read(CONFIG_PATH)
if not read_ok:
print(f"[Config] WARNING: could not read config at {CONFIG_PATH}; using in-image defaults where possible.")
# Intents (enable Members + Message Content in Dev Portal)
intents = discord.Intents.default()
@ -24,9 +29,12 @@ intents.reactions = True
intents.emojis_and_stickers = True
intents.voice_states = True
# Data file path (prefer INI, else env, else sensible default)
data_file = config['DEFAULT'].get('data_file', os.getenv('SHAI_DATA', '/data/data.json'))
bot = commands.Bot(command_prefix='!', intents=intents)
bot.config = config
bot.data_manager = DataManager(config['DEFAULT']['data_file'])
bot.data_manager = DataManager(data_file)
async def _guild_selfcheck(g: discord.Guild, cfg):
problems = []

View File

@ -39,6 +39,8 @@ class DataManager:
return default
def _safe_write(self, data: dict):
# ensure parent dir exists
os.makedirs(os.path.dirname(self.json_path) or ".", exist_ok=True)
tmp = self.json_path + ".tmp"
with open(tmp, 'w') as f:
json.dump(data, f, indent=4)

View File

@ -1,20 +1,16 @@
version: "3.8"
services:
shaiwatcher:
build:
context: .
dockerfile: dockerfile
container_name: shaiwatcher
build: .
restart: unless-stopped
environment:
- DISCORD_TOKEN=${DISCORD_TOKEN}
# optional: set timezone
- TZ=UTC
# optional only if you want non-default locations
# - SHAI_CONFIG=/config/settings.conf
# - SHAI_DATA=/data/data.json
volumes:
# Persist your JSON data here
- shaiwatcher_data:/app/data
# use your repos settings.conf at runtime
- ./settings.conf:/app/settings.conf:ro
- ./config:/config # will contain settings.conf
- shaiwatcher_data:/data # persistent data.json
# if you deploy to a remote docker host via Portainer: no extra ports needed
volumes:
shaiwatcher_data:

30
docker-entrypoint.sh Normal file
View File

@ -0,0 +1,30 @@
#!/bin/sh
set -e
# Defaults (can be overridden)
: "${SHAI_CONFIG:=/config/settings.conf}"
: "${SHAI_DATA:=/data/data.json}"
# Seed /config/settings.conf on first run if it doesn't exist
if [ ! -f "$SHAI_CONFIG" ]; then
mkdir -p "$(dirname "$SHAI_CONFIG")"
if [ -f /app/example/settings.conf ]; then
cp /app/example/settings.conf "$SHAI_CONFIG"
echo "Seeded default settings to $SHAI_CONFIG"
else
# Fall back: generate minimal config so the app can boot
cat > "$SHAI_CONFIG" <<EOF
[DEFAULT]
data_file = ${SHAI_DATA}
EOF
echo "Generated minimal $SHAI_CONFIG"
fi
fi
# Ensure data directory exists
mkdir -p "$(dirname "$SHAI_DATA")"
# Make path visible to the app (bot.py will still read the INI)
export SHAI_CONFIG SHAI_DATA
exec "$@"

View File

@ -1,23 +1,26 @@
# Simple, small image
FROM python:3.11-slim
ENV PYTHONUNBUFFERED=1 \
PIP_NO_CACHE_DIR=1
# System deps (add git if you need it)
RUN apt-get update && apt-get install -y --no-install-recommends \
ca-certificates tzdata && \
rm -rf /var/lib/apt/lists/*
WORKDIR /app
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1
# Install deps first (better cache)
# deps first
COPY requirements.txt .
RUN pip install -r requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Copy the app
COPY . .
# app code
COPY . /app
# The bot reads settings.conf from /app/settings.conf
# The data file should live in /app/data (mounted as a volume)
CMD ["python", "-u", "bot.py"]
# runtime dirs + seed default config path (actual seeding done in entrypoint)
RUN mkdir -p /config /data
# runtime env defaults (can be overridden by compose/env)
ENV SHAI_CONFIG=/config/settings.conf \
SHAI_DATA=/data/data.json
# small, explicit entrypoint
COPY docker-entrypoint.sh /usr/local/bin/entrypoint
RUN chmod +x /usr/local/bin/entrypoint
ENTRYPOINT ["entrypoint"]
CMD ["python","-u","/app/bot.py"]