docker ready deployment

This commit is contained in:
2026-01-12 18:12:30 +01:00
parent 462f8a688b
commit 67b6410687
13 changed files with 253 additions and 35 deletions

2
discord/.gitignore vendored
View File

@@ -1 +1 @@
config.py
.env.bot

9
discord/Dockerfile Normal file
View File

@@ -0,0 +1,9 @@
FROM python:3.12-slim
WORKDIR /app
RUN apt-get update && apt-get install -y libpq-dev gcc && rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
RUN useradd -m botuser
USER botuser
CMD ["python", "discord_bot.py"]

View File

@@ -1,3 +0,0 @@
TOKEN =
ROLE_NAME = "Participant"
DATABASE_FILE =

View File

@@ -1,7 +1,7 @@
import discord
from discord import app_commands, ui
import sqlite3
from config import TOKEN, ROLE_NAME, DATABASE_FILE
import psycopg2
import os
class VerificationModal(ui.Modal, title='Server Verification'):
token_input = ui.TextInput(
@@ -15,31 +15,40 @@ class VerificationModal(ui.Modal, title='Server Verification'):
async def on_submit(self, interaction: discord.Interaction):
token = self.token_input.value
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute("SELECT id, discord_user_id FROM participant WHERE discord_token = ?", (token,))
result = cursor.fetchone()
try:
# Connect using your Postgres credentials
conn = psycopg2.connect(database=os.environ.get("POSTGRES_DB"), username=os.environ.get("POSTGRES_USER"), password=os.environ.get("POSTGRES_PASSWORD"))
with conn.cursor() as cursor:
# Postgres uses %s as placeholders instead of ?
cursor.execute("SELECT id, discord_user_id FROM participant WHERE discord_token = %s", (token,))
result = cursor.fetchone()
if not result:
await interaction.response.send_message("Invalid token. Check your email.", ephemeral=True)
return
if not result:
await interaction.response.send_message("Invalid token. Check your email.", ephemeral=True)
return
db_id, existing_user_id = result
if existing_user_id:
await interaction.response.send_message("This token is already linked to an account.", ephemeral=True)
return
db_id, existing_user_id = result
if existing_user_id:
await interaction.response.send_message("This token is already linked to an account.", ephemeral=True)
return
# Update DB and Role
cursor.execute("UPDATE participant SET discord_user_id = ? WHERE id = ?", (str(interaction.user.id), db_id))
conn.commit()
conn.close()
# Update DB
cursor.execute("UPDATE participant SET discord_user_id = %s WHERE id = %s", (str(interaction.user.id), db_id))
conn.commit()
conn.close()
role = discord.utils.get(interaction.guild.roles, name=ROLE_NAME)
if role:
await interaction.user.add_roles(role)
await interaction.response.send_message("Verified! Welcome to the server.", ephemeral=True)
else:
await interaction.response.send_message("Verification successful, but role not found. Contact staff.", ephemeral=True)
# Role Logic
role = discord.utils.get(interaction.guild.roles, name=os.environ.get("ROLE_NAME"))
if role:
await interaction.user.add_roles(role)
await interaction.response.send_message("Verified! Welcome to the server.", ephemeral=True)
else:
await interaction.response.send_message("Verification successful, but role not found. Contact staff.", ephemeral=True)
except Exception as e:
print(f"Database Error: {e}")
await interaction.response.send_message("A database error occurred. Please try again later.", ephemeral=True)
class PersistentView(ui.View):
def __init__(self):
@@ -85,4 +94,4 @@ async def setup_welcome(interaction: discord.Interaction):
await interaction.channel.send(embed=embed, view=PersistentView())
await interaction.response.send_message("Setup complete!", ephemeral=True)
bot.run(TOKEN)
bot.run(os.environ.get("TOKEN"))

36
discord/requirements.txt Normal file
View File

@@ -0,0 +1,36 @@
# This file was autogenerated by uv via the following command:
# uv export --format requirements-txt -o requirements.txt --no-hashes
aiohappyeyeballs==2.6.1
# via aiohttp
aiohttp==3.13.3
# via discord-py
aiosignal==1.4.0
# via aiohttp
attrs==25.4.0
# via aiohttp
audioop-lts==0.2.2 ; python_full_version >= '3.13'
# via discord-py
discord==2.3.2
# via test
discord-py==2.6.4
# via discord
frozenlist==1.8.0
# via
# aiohttp
# aiosignal
idna==3.11
# via yarl
multidict==6.7.0
# via
# aiohttp
# yarl
propcache==0.4.1
# via
# aiohttp
# yarl
psycopg2-binary==2.9.11
# via test
typing-extensions==4.15.0 ; python_full_version < '3.13'
# via aiosignal
yarl==1.22.0
# via aiohttp