modified: app.py
modified: requirements.txt modified: templates/index.html
This commit is contained in:
71
app.py
71
app.py
@@ -3,6 +3,7 @@ __all__ = ["Discordbot-chatai-webpanel (Discord)"]
|
|||||||
__author__ = "SimolZimol"
|
__author__ = "SimolZimol"
|
||||||
|
|
||||||
from flask import Flask, render_template, redirect, url_for, request, session, jsonify, send_file
|
from flask import Flask, render_template, redirect, url_for, request, session, jsonify, send_file
|
||||||
|
from requests_oauthlib import OAuth2Session
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import psutil
|
import psutil
|
||||||
@@ -21,6 +22,13 @@ DB_USER = os.getenv("DB_USER")
|
|||||||
DB_PASS = os.getenv("DB_PASSWORD")
|
DB_PASS = os.getenv("DB_PASSWORD")
|
||||||
DB_NAME = os.getenv("DB_DATABASE")
|
DB_NAME = os.getenv("DB_DATABASE")
|
||||||
|
|
||||||
|
DISCORD_CLIENT_ID = os.getenv("DISCORD_CLIENT_ID")
|
||||||
|
DISCORD_CLIENT_SECRET = os.getenv("DISCORD_CLIENT_SECRET")
|
||||||
|
DISCORD_REDIRECT_URI = os.getenv("DISCORD_REDIRECT_URI")
|
||||||
|
DISCORD_OAUTH2_URL = "https://discord.com/api/oauth2/authorize"
|
||||||
|
DISCORD_TOKEN_URL = "https://discord.com/api/oauth2/token"
|
||||||
|
DISCORD_API_URL = "https://discord.com/api/users/@me"
|
||||||
|
|
||||||
# Globale Variablen für die Intros
|
# Globale Variablen für die Intros
|
||||||
INTRO_FILE = "introduction.txt"
|
INTRO_FILE = "introduction.txt"
|
||||||
ASKNOTES_INTRO_FILE = "asknotesintro.txt"
|
ASKNOTES_INTRO_FILE = "asknotesintro.txt"
|
||||||
@@ -75,28 +83,67 @@ def get_db_connection():
|
|||||||
database=DB_NAME
|
database=DB_NAME
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def make_discord_session(token=None, state=None):
|
||||||
|
return OAuth2Session(
|
||||||
|
DISCORD_CLIENT_ID,
|
||||||
|
token=token,
|
||||||
|
state=state,
|
||||||
|
redirect_uri=DISCORD_REDIRECT_URI,
|
||||||
|
scope=["identify"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
if "username" in session:
|
if "username" in session:
|
||||||
return render_template("index.html", bot_running=bot_status())
|
return render_template("index.html", bot_running=bot_status())
|
||||||
return redirect(url_for("login"))
|
return redirect(url_for("login"))
|
||||||
|
|
||||||
@app.route("/login", methods=["GET", "POST"])
|
@app.route("/login")
|
||||||
def login():
|
def login():
|
||||||
if request.method == "POST":
|
"""Startet den Discord-OAuth2-Flow."""
|
||||||
username = request.form["username"]
|
discord = make_discord_session()
|
||||||
password = request.form["password"]
|
authorization_url, state = discord.authorization_url(DISCORD_OAUTH2_URL)
|
||||||
if username == os.getenv("ADMIN_USER") and password == os.getenv("ADMIN_PASS"):
|
|
||||||
session["username"] = username
|
session['oauth_state'] = state
|
||||||
return redirect(url_for("index"))
|
return redirect(authorization_url)
|
||||||
else:
|
|
||||||
return "Invalid credentials!"
|
@app.route("/callback")
|
||||||
return render_template("login.html")
|
def callback():
|
||||||
|
"""Verarbeitet den OAuth2-Rückruf von Discord."""
|
||||||
|
discord = make_discord_session(state=session.get("oauth_state"))
|
||||||
|
token = discord.fetch_token(
|
||||||
|
DISCORD_TOKEN_URL,
|
||||||
|
client_secret=DISCORD_CLIENT_SECRET,
|
||||||
|
authorization_response=request.url,
|
||||||
|
)
|
||||||
|
|
||||||
|
session['oauth_token'] = token
|
||||||
|
|
||||||
|
# User-Informationen von Discord abrufen
|
||||||
|
user_info = discord.get(DISCORD_API_URL).json()
|
||||||
|
|
||||||
|
# Speichere die Benutzerinformationen in der Session
|
||||||
|
session['discord_user'] = user_info
|
||||||
|
|
||||||
|
return redirect(url_for("dashboard"))
|
||||||
|
|
||||||
|
@app.route("/dashboard")
|
||||||
|
def dashboard():
|
||||||
|
"""Das Dashboard nach erfolgreicher Authentifizierung."""
|
||||||
|
if "discord_user" not in session:
|
||||||
|
return redirect(url_for("login"))
|
||||||
|
|
||||||
|
user_info = session['discord_user']
|
||||||
|
return render_template("dashboard.html", user_info=user_info)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/logout")
|
@app.route("/logout")
|
||||||
def logout():
|
def logout():
|
||||||
session.pop("username", None)
|
"""Löscht die Benutzersitzung und meldet den Benutzer ab."""
|
||||||
return redirect(url_for("login"))
|
session.pop('discord_user', None)
|
||||||
|
session.pop('oauth_token', None)
|
||||||
|
return redirect(url_for('login'))
|
||||||
|
|
||||||
@app.route("/start_bot")
|
@app.route("/start_bot")
|
||||||
def start():
|
def start():
|
||||||
|
|||||||
@@ -13,3 +13,4 @@ pdfplumber
|
|||||||
python-dotenv
|
python-dotenv
|
||||||
flask
|
flask
|
||||||
psutil
|
psutil
|
||||||
|
requests_oauthlib
|
||||||
@@ -1,37 +1,22 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>Admin Panel</title>
|
<title>Login</title>
|
||||||
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
|
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="container mt-5">
|
<div class="container mt-5">
|
||||||
<h1 class="text-center">Discord Bot Admin Panel</h1>
|
<h1 class="text-center">Login with Discord</h1>
|
||||||
<div class="card mt-4">
|
<div class="text-center mt-4">
|
||||||
<div class="card-body">
|
<a href="{{ url_for('login') }}" class="btn btn-primary">
|
||||||
<h5 class="card-title">Bot Status</h5>
|
<img src="https://discord.com/assets/847541504914fd33810e70a0ea73177e.svg" width="20" alt="Discord Logo">
|
||||||
<p class="card-text">Status:
|
Login with Discord
|
||||||
<span class="badge badge-{{ 'success' if bot_running else 'danger' }}">
|
</a>
|
||||||
{{ "Running" if bot_running else "Stopped" }}
|
|
||||||
</span>
|
|
||||||
</p>
|
|
||||||
<a href="{{ url_for('start') }}"
|
|
||||||
class="btn btn-primary btn-block {{ 'disabled' if bot_running else '' }}">Start Bot</a>
|
|
||||||
<a href="{{ url_for('stop') }}"
|
|
||||||
class="btn btn-danger btn-block {{ 'disabled' if not bot_running else '' }}">Stop Bot</a>
|
|
||||||
<a href="{{ url_for('settings') }}" class="btn btn-secondary btn-block">Settings</a>
|
|
||||||
<a href="{{ url_for('view_logs') }}" class="btn btn-warning btn-block">View Logs</a>
|
|
||||||
<a href="{{ url_for('users') }}" class="btn btn-info btn-block">User Management</a>
|
|
||||||
<a href="{{ url_for('logout') }}" class="btn btn-outline-secondary btn-block">Logout</a>
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
|
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
Reference in New Issue
Block a user