modified: app.py
This commit is contained in:
184
app.py
184
app.py
@@ -1,7 +1,12 @@
|
||||
__version__ = "dev-0.4.6"
|
||||
__all__ = ["Discordbot-chatai-webpanel (Discord)"]
|
||||
__author__ = "SimolZimol"
|
||||
|
||||
from flask import Flask, render_template, redirect, url_for, request, session, jsonify, send_file, flash
|
||||
from requests_oauthlib import OAuth2Session
|
||||
import os
|
||||
import subprocess
|
||||
import psutil
|
||||
import mysql.connector
|
||||
from datetime import datetime
|
||||
|
||||
@@ -25,6 +30,11 @@ DISCORD_TOKEN_URL = "https://discord.com/api/oauth2/token"
|
||||
DISCORD_API_URL = "https://discord.com/api/users/@me"
|
||||
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
|
||||
|
||||
# Globale Variablen für die Intros
|
||||
INTRO_FILE = "introduction.txt"
|
||||
ASKNOTES_INTRO_FILE = "asknotesintro.txt"
|
||||
|
||||
# Speichern der Prozess-ID
|
||||
bot_process = None
|
||||
|
||||
def bot_status():
|
||||
@@ -47,11 +57,23 @@ def stop_bot():
|
||||
global bot_process
|
||||
if bot_process and bot_status():
|
||||
bot_process.terminate()
|
||||
bot_process.wait()
|
||||
bot_process.wait() # Warten, bis der Prozess beendet ist
|
||||
bot_process = None
|
||||
else:
|
||||
print("Bot läuft nicht.")
|
||||
|
||||
def load_text_file(file_path):
|
||||
"""Lädt den Inhalt einer Textdatei."""
|
||||
if os.path.exists(file_path):
|
||||
with open(file_path, 'r', encoding='utf-8') as file:
|
||||
return file.read()
|
||||
return ""
|
||||
|
||||
def save_text_file(file_path, content):
|
||||
"""Speichert den Inhalt in einer Textdatei."""
|
||||
with open(file_path, 'w', encoding='utf-8') as file:
|
||||
file.write(content)
|
||||
|
||||
def get_db_connection():
|
||||
"""Stellt eine Verbindung zur MySQL-Datenbank her."""
|
||||
return mysql.connector.connect(
|
||||
@@ -77,27 +99,32 @@ def is_global_admin():
|
||||
user_info = session["discord_user"]
|
||||
user_id = user_info["id"]
|
||||
|
||||
# Überprüfe die globalen Admin-Rechte des Benutzers
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
|
||||
cursor.execute("SELECT global_permission FROM bot_data WHERE user_id = %s", (user_id,))
|
||||
bot_data = cursor.fetchone()
|
||||
user_data = cursor.fetchone()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
if bot_data and bot_data["global_permission"] >= 8:
|
||||
if user_data and user_data["global_permission"] >= 8:
|
||||
return True
|
||||
return False
|
||||
|
||||
def is_server_admin(guild_id):
|
||||
"""Überprüft, ob der Benutzer Admin-Rechte auf einem bestimmten Server hat."""
|
||||
"""Überprüft, ob der Benutzer Admin-Rechte auf einem bestimmten Server (Guild) hat."""
|
||||
if "discord_user" in session:
|
||||
user_info = session["discord_user"]
|
||||
user_id = user_info["id"]
|
||||
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
|
||||
cursor.execute("SELECT permission FROM user_data WHERE user_id = %s AND guild_id = %s", (user_id, guild_id))
|
||||
user_data = cursor.fetchone()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
@@ -131,104 +158,161 @@ def callback():
|
||||
|
||||
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
|
||||
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
cursor.execute("SELECT global_permission FROM bot_data WHERE user_id = %s", (user_info["id"],))
|
||||
bot_data = cursor.fetchone()
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
if bot_data and bot_data["global_permission"] >= 8:
|
||||
# Überprüfe, ob der Benutzer globale Admin-Rechte hat
|
||||
if is_global_admin():
|
||||
return redirect(url_for("global_admin_dashboard"))
|
||||
else:
|
||||
return redirect(url_for("select_guild"))
|
||||
|
||||
@app.route("/select_guild")
|
||||
def select_guild():
|
||||
"""Lässt den Benutzer den Server auswählen, auf dem er aktiv sein möchte."""
|
||||
"""Bietet dem Benutzer eine Liste der Server (Guilds), auf denen er Mitglied ist."""
|
||||
if "discord_user" in session:
|
||||
user_info = session["discord_user"]
|
||||
user_id = user_info["id"]
|
||||
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
cursor.execute("SELECT guild_id FROM user_data WHERE user_id = %s", (user_id,))
|
||||
|
||||
cursor.execute("SELECT DISTINCT guild_id FROM user_data WHERE user_id = %s", (user_id,))
|
||||
guilds = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
if guilds:
|
||||
return render_template("select_guild.html", guilds=guilds)
|
||||
else:
|
||||
return "No server found for this user.", 404
|
||||
return redirect(url_for("landing_page"))
|
||||
|
||||
@app.route("/set_guild/<int:guild_id>")
|
||||
def set_guild(guild_id):
|
||||
"""Setzt den aktuellen Server (guild_id) für den Benutzer."""
|
||||
session["guild_id"] = guild_id
|
||||
return redirect(url_for("server_dashboard", guild_id=guild_id))
|
||||
|
||||
@app.route("/global_admin_dashboard")
|
||||
def global_admin_dashboard():
|
||||
"""Globales Admin-Dashboard für globale Administratoren."""
|
||||
if is_global_admin():
|
||||
user_info = session["discord_user"]
|
||||
return render_template("global_admin_dashboard.html", user_info=user_info, bot_running=bot_status())
|
||||
return redirect(url_for("landing_page"))
|
||||
return redirect(url_for("login"))
|
||||
|
||||
@app.route("/server_dashboard/<int:guild_id>")
|
||||
def server_dashboard(guild_id):
|
||||
"""Server-spezifisches Dashboard für den aktuellen Server (guild_id)."""
|
||||
"""Zeigt das Server-Admin-Dashboard an (nur für Admins eines bestimmten Servers)."""
|
||||
if is_server_admin(guild_id):
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
|
||||
# Hole Server-spezifische Daten (Giveaways, Punkte usw.)
|
||||
cursor.execute("SELECT * FROM giveaway_data WHERE guild_id = %s", (guild_id,))
|
||||
giveaways = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
return render_template("server_dashboard.html", guild_id=guild_id, giveaways=giveaways)
|
||||
return redirect(url_for("select_guild"))
|
||||
|
||||
@app.route("/global_admin_dashboard")
|
||||
def global_admin_dashboard():
|
||||
"""Zeigt das globale Admin-Dashboard an (nur für globale Admins)."""
|
||||
if is_global_admin():
|
||||
user_info = session["discord_user"]
|
||||
return render_template("server_dashboard.html", user_info=user_info, guild_id=guild_id, bot_running=bot_status())
|
||||
return redirect(url_for("landing_page"))
|
||||
return render_template("global_admin_dashboard.html", user_info=user_info, bot_running=bot_status())
|
||||
return redirect(url_for("select_guild"))
|
||||
|
||||
@app.route("/start_bot")
|
||||
def start():
|
||||
if is_global_admin():
|
||||
start_bot()
|
||||
return redirect(url_for("global_admin_dashboard"))
|
||||
user_info = session["discord_user"]
|
||||
return render_template("global_admin_dashboard.html", user_info=user_info, bot_running=bot_status())
|
||||
return redirect(url_for("landing_page"))
|
||||
|
||||
@app.route("/stop_bot")
|
||||
def stop():
|
||||
if is_global_admin():
|
||||
stop_bot()
|
||||
return redirect(url_for("global_admin_dashboard"))
|
||||
user_info = session["discord_user"]
|
||||
return render_template("global_admin_dashboard.html", user_info=user_info, bot_running=bot_status())
|
||||
return redirect(url_for("landing_page"))
|
||||
|
||||
@app.route("/server_admin/giveaways/<int:guild_id>")
|
||||
def server_admin_giveaways(guild_id):
|
||||
"""Zeigt eine Liste der Giveaways für einen bestimmten Server an."""
|
||||
@app.route("/server_settings/<int:guild_id>", methods=["GET", "POST"])
|
||||
def server_settings(guild_id):
|
||||
"""Server-spezifische Einstellungen für den Server-Admin."""
|
||||
if is_server_admin(guild_id):
|
||||
if request.method == "POST":
|
||||
introduction = request.form.get("introduction")
|
||||
asknotes_introduction = request.form.get("asknotes_introduction")
|
||||
|
||||
# Speichern der Intros
|
||||
save_text_file(INTRO_FILE, introduction)
|
||||
save_text_file(ASKNOTES_INTRO_FILE, asknotes_introduction)
|
||||
|
||||
return redirect(url_for("server_settings", guild_id=guild_id))
|
||||
|
||||
# Laden der aktuellen Inhalte aus den Textdateien
|
||||
introduction = load_text_file(INTRO_FILE)
|
||||
asknotes_introduction = load_text_file(ASKNOTES_INTRO_FILE)
|
||||
|
||||
return render_template("server_settings.html", guild_id=guild_id, introduction=introduction, asknotes_introduction=asknotes_introduction)
|
||||
return redirect(url_for("select_guild"))
|
||||
|
||||
@app.route("/global_settings", methods=["GET", "POST"])
|
||||
def global_settings():
|
||||
"""Globale Einstellungen für den Bot-Admin."""
|
||||
if is_global_admin():
|
||||
if request.method == "POST":
|
||||
introduction = request.form.get("introduction")
|
||||
asknotes_introduction = request.form.get("asknotes_introduction")
|
||||
|
||||
# Speichern der Intros
|
||||
save_text_file(INTRO_FILE, introduction)
|
||||
save_text_file(ASKNOTES_INTRO_FILE, asknotes_introduction)
|
||||
|
||||
return redirect(url_for("global_settings"))
|
||||
|
||||
# Laden der aktuellen Inhalte aus den Textdateien
|
||||
introduction = load_text_file(INTRO_FILE)
|
||||
asknotes_introduction = load_text_file(ASKNOTES_INTRO_FILE)
|
||||
|
||||
return render_template("global_settings.html", introduction=introduction, asknotes_introduction=asknotes_introduction)
|
||||
return redirect(url_for("global_admin_dashboard"))
|
||||
|
||||
@app.route("/logout")
|
||||
def logout():
|
||||
"""Löscht die Benutzersitzung und meldet den Benutzer ab."""
|
||||
session.pop('discord_user', None)
|
||||
session.pop('oauth_token', None)
|
||||
return redirect(url_for('landing_page'))
|
||||
|
||||
@app.route("/server_giveaways/<int:guild_id>")
|
||||
def server_giveaways(guild_id):
|
||||
"""Zeigt die Giveaways für einen spezifischen Server an."""
|
||||
if is_server_admin(guild_id):
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
|
||||
cursor.execute("SELECT * FROM giveaway_data WHERE guild_id = %s", (guild_id,))
|
||||
giveaways = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
return render_template("server_admin_giveaways.html", giveaways=giveaways, guild_id=guild_id)
|
||||
return redirect(url_for("landing_page"))
|
||||
|
||||
@app.route("/users")
|
||||
def users():
|
||||
"""Zeigt die Liste der Benutzer an."""
|
||||
return render_template("server_giveaways.html", guild_id=guild_id, giveaways=giveaways)
|
||||
return redirect(url_for("select_guild"))
|
||||
|
||||
@app.route("/global_giveaways", methods=["GET", "POST"])
|
||||
def global_giveaways():
|
||||
"""Zeigt globale Giveaways an und ermöglicht globale Änderungen."""
|
||||
if is_global_admin():
|
||||
connection = get_db_connection()
|
||||
cursor = connection.cursor(dictionary=True)
|
||||
cursor.execute("SELECT user_id, permission, points, ban FROM user_data")
|
||||
users = cursor.fetchall()
|
||||
|
||||
sort_field = request.args.get("sort", "id") # Standardmäßig nach 'id' sortieren
|
||||
order = request.args.get("order", "asc") # Standardmäßig aufsteigend sortieren
|
||||
|
||||
cursor.execute(f"SELECT * FROM giveaway_data ORDER BY {sort_field} {order}")
|
||||
giveaways = cursor.fetchall()
|
||||
|
||||
cursor.close()
|
||||
connection.close()
|
||||
return render_template("users.html", users=users)
|
||||
return redirect(url_for("landing_page"))
|
||||
|
||||
# Weitere Routen für Server-spezifische Funktionen, Punkteverwaltung, Giveaways etc.
|
||||
return render_template("global_giveaways.html", giveaways=giveaways)
|
||||
return redirect(url_for("global_admin_dashboard"))
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host="0.0.0.0", port=5000, debug=True)
|
||||
|
||||
Reference in New Issue
Block a user