2025-05-14 16:14:22 +00:00
|
|
|
#!/usr/bin/env python3
|
2025-05-14 16:08:22 +00:00
|
|
|
import os
|
|
|
|
from flask import Flask, render_template, request, redirect, flash
|
2025-05-14 16:14:22 +00:00
|
|
|
from sqlalchemy import create_engine
|
|
|
|
from sqlalchemy.orm import sessionmaker
|
2025-05-14 15:02:34 +00:00
|
|
|
|
2025-05-14 16:14:22 +00:00
|
|
|
# import ORM models & Base from your bot module
|
|
|
|
from discord_lnbits_bot import Base, Config, Plan
|
2025-05-14 15:02:34 +00:00
|
|
|
|
2025-05-14 16:14:22 +00:00
|
|
|
# Flask setup
|
2025-05-14 16:08:22 +00:00
|
|
|
app = Flask(__name__)
|
2025-05-14 16:14:22 +00:00
|
|
|
app.secret_key = os.environ["FLASK_SECRET"]
|
2025-05-14 15:02:34 +00:00
|
|
|
|
2025-05-14 16:14:22 +00:00
|
|
|
# Database setup
|
|
|
|
DATABASE_URL = os.environ["DATABASE_URL"]
|
|
|
|
engine = create_engine(DATABASE_URL, future=True)
|
|
|
|
SessionLocal = sessionmaker(bind=engine, expire_on_commit=False)
|
|
|
|
Base.metadata.create_all(engine)
|
2025-05-14 15:02:34 +00:00
|
|
|
|
2025-05-14 16:14:22 +00:00
|
|
|
@app.route("/", methods=["GET", "POST"])
|
|
|
|
def settings():
|
|
|
|
db = SessionLocal()
|
|
|
|
if request.method == "POST":
|
|
|
|
# save core config keys
|
|
|
|
for key in ("discord_token", "guild_id", "lnbits_url", "lnbits_api_key"):
|
|
|
|
val = request.form.get(key, "").strip()
|
|
|
|
cfg = db.get(Config, key)
|
|
|
|
if cfg:
|
|
|
|
cfg.value = val
|
|
|
|
else:
|
|
|
|
db.add(Config(key=key, value=val))
|
|
|
|
db.commit()
|
|
|
|
flash("Configuration saved.", "success")
|
|
|
|
return redirect("/")
|
|
|
|
|
|
|
|
configs = db.query(Config).all()
|
|
|
|
plans = db.query(Plan).all()
|
|
|
|
db.close()
|
|
|
|
return render_template("settings.html", configs=configs, plans=plans)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(host="0.0.0.0", port=3000)
|