From f25f7cfb15c2c94a4318bb64e8c538fc2ae7c1d7 Mon Sep 17 00:00:00 2001 From: MrWhiff Date: Tue, 9 Dec 2025 20:21:47 +0100 Subject: [PATCH] AstraOS Ver. 1.6: - DC Bots werden automatisch nach restart gestartet --- AstraOS/astra_os.py | 47 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/AstraOS/astra_os.py b/AstraOS/astra_os.py index d278fab..54a605f 100644 --- a/AstraOS/astra_os.py +++ b/AstraOS/astra_os.py @@ -127,6 +127,44 @@ except Exception as e: db = None accounts_collection = None logs_collection = None + +def start_bots_on_startup(): + if discord_bots_collection is None: + print("WARNUNG: Bot-Autostart übersprungen, da keine DB-Verbindung besteht.") + return + + bots_to_start = list(discord_bots_collection.find({'status': 'running'})) + if not bots_to_start: + print("INFO: Keine Bots für den Autostart markiert.") + return + + print(f"INFO: Starte {len(bots_to_start)} Bot(s) automatisch...") + for bot_info in bots_to_start: + bot_id = str(bot_info['_id']) + filepath = bot_info.get('filepath') + + if not os.path.exists(filepath): + print(f"FEHLER: Bot-Datei für '{bot_info['name']}' nicht gefunden: {filepath}") + continue + + try: + process = subprocess.Popen( + ['python', '-u', filepath], + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + text=True, + encoding='utf-8', + errors='replace' + ) + running_bots[bot_id] = {'process': process, 'history': []} + thread = threading.Thread(target=stream_bot_output, args=(bot_id, process)) + thread.daemon = True + thread.start() + print(f"INFO: Bot '{bot_info['name']}' (PID: {process.pid}) wurde gestartet.") + except Exception as e: + print(f"FEHLER: Bot '{bot_info['name']}' konnte nicht gestartet werden: {e}") + + try: tickets_client = MongoClient(os.environ.get("MONGO_URI")) tickets_db = tickets_client["chainsaw_dc_data"] @@ -722,6 +760,8 @@ def stream_bot_output(bot_id, process): process.wait() if bot_id in running_bots: del running_bots[bot_id] + if discord_bots_collection: + discord_bots_collection.update_one({'_id': ObjectId(bot_id)}, {'$set': {'status': 'stopped'}}) socketio.emit('status_update', {'bot_id': bot_id, 'status': 'stopped'}, room=bot_id) socketio.emit('console_output', {'bot_id': bot_id, 'type': 'system', 'data': '--- Bot-Prozess beendet ---'}, room=bot_id) @app.route('/discord_bots') @@ -751,7 +791,8 @@ def upload_bot(): 'name': bot_name, 'filename': filename, 'filepath': filepath, - 'created_at': datetime.utcnow() + 'created_at': datetime.utcnow(), + 'status': 'stopped' }) flash(f"Bot '{bot_name}' wurde erfolgreich hochgeladen.", "success") return redirect(url_for('discord_bots')) @@ -833,6 +874,7 @@ def handle_toggle_bot(data): thread = threading.Thread(target=stream_bot_output, args=(bot_id, process)) thread.daemon = True thread.start() + discord_bots_collection.update_one({'_id': ObjectId(bot_id)}, {'$set': {'status': 'running'}}) emit('status_update', {'bot_id': bot_id, 'status': 'running'}, room=bot_id) emit('console_output', {'type': 'system', 'data': f'--- Bot-Prozess gestartet (PID: {process.pid}) ---'}, room=bot_id) except Exception as e: @@ -846,6 +888,7 @@ def handle_toggle_bot(data): process.kill() if bot_id in running_bots: del running_bots[bot_id] + discord_bots_collection.update_one({'_id': ObjectId(bot_id)}, {'$set': {'status': 'stopped'}}) emit('status_update', {'bot_id': bot_id, 'status': 'stopped'}, room=bot_id) emit('console_output', {'type': 'system', 'data': '--- Bot-Prozess wird gestoppt... ---'}, room=bot_id) @app.route('/maintenance') @@ -926,4 +969,6 @@ def update_maintenance(): if __name__ == '__main__': app.config['SESSION_COOKIE_SECURE'] = False print("INFO: Starte den Server im Entwicklungsmodus ohne HTTPS.") + with app.app_context(): + start_bots_on_startup() socketio.run(app, host='0.0.0.0', port=5000, debug=True, allow_unsafe_werkzeug=True)