modified: app.py

This commit is contained in:
SimolZimol
2024-09-06 16:50:00 +02:00
parent 6170ad8cd7
commit 144377c0db

28
app.py
View File

@@ -38,15 +38,14 @@ ASKNOTES_INTRO_FILE = "asknotesintro.txt"
bot_process = None
def bot_status():
"""Überprüft, ob der Bot läuft, indem der Prozess nach dem Skript-Namen durchsucht wird."""
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
# Überprüfen, ob 'bot.py' im Befehlszeilen-Argument des Prozesses vorhanden ist
if 'python' in proc.info['name'] and 'bot.py' in proc.info['cmdline']:
return True
return False
"""Überprüft, ob der Bot läuft."""
global bot_process
if bot_process is None:
return False
return bot_process.poll() is None # None bedeutet, dass der Prozess noch läuft
def start_bot():
"""Startet den Bot, falls er nicht bereits läuft."""
"""Startet den Bot."""
global bot_process
if not bot_status():
bot_process = subprocess.Popen(["python", "bot.py"], cwd=os.path.dirname(os.path.abspath(__file__)))
@@ -56,13 +55,10 @@ def start_bot():
def stop_bot():
"""Stoppt den Bot."""
global bot_process
if bot_status():
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
if 'python' in proc.info['name'] and 'bot.py' in proc.info['cmdline']:
proc.terminate()
proc.wait()
print("Bot wurde gestoppt.")
bot_process = None
if bot_process and bot_status():
bot_process.terminate()
bot_process.wait() # Warten, bis der Prozess beendet ist
bot_process = None
else:
print("Bot läuft nicht.")
@@ -203,7 +199,7 @@ def admin_dashboard():
connection.close()
if user_data and user_data["permission"] >= 8:
return render_template("admin_dashboard.html", user_info=user_info)
return render_template("admin_dashboard.html", user_info=user_info, bot_running=bot_status())
else:
return redirect(url_for("user_dashboard"))
return redirect(url_for("landing_page"))
@@ -239,7 +235,7 @@ def logout():
@app.route("/start_bot")
def start():
if is_admin():
start_bot()
start_bot()
user_info = session["discord_user"]
return render_template("admin_dashboard.html", user_info=user_info)
return redirect(url_for("landing_page"))