89 lines
2.6 KiB
Python
89 lines
2.6 KiB
Python
|
|
"""
|
||
|
|
bot_launcher.py
|
||
|
|
Launches both the Discord bot and web UI together
|
||
|
|
"""
|
||
|
|
|
||
|
|
import os
|
||
|
|
import sys
|
||
|
|
import threading
|
||
|
|
import time
|
||
|
|
import signal
|
||
|
|
import logging
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
# Add src directory to path
|
||
|
|
src_path = Path(__file__).parent / 'src'
|
||
|
|
sys.path.insert(0, str(src_path))
|
||
|
|
|
||
|
|
def start_web_ui():
|
||
|
|
"""Start the web UI server in a separate thread"""
|
||
|
|
try:
|
||
|
|
from web_ui import run_web_server
|
||
|
|
port = int(os.getenv('WEB_PORT', 8080))
|
||
|
|
debug = os.getenv('DEBUG', 'false').lower() == 'true'
|
||
|
|
|
||
|
|
run_web_server(host='0.0.0.0', port=port, debug=debug)
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ Failed to start web UI: {e}")
|
||
|
|
if 'flask' in str(e).lower():
|
||
|
|
print("💡 Install Flask to use the web UI: pip install flask")
|
||
|
|
elif 'port' in str(e).lower() or 'address' in str(e).lower():
|
||
|
|
print(f"💡 Port {port} may be in use. Try a different port with WEB_PORT environment variable")
|
||
|
|
|
||
|
|
def start_discord_bot():
|
||
|
|
"""Start the Discord bot"""
|
||
|
|
try:
|
||
|
|
# Change to src directory for bot execution
|
||
|
|
original_cwd = os.getcwd()
|
||
|
|
os.chdir(src_path)
|
||
|
|
|
||
|
|
print("🤖 Starting Discord bot...")
|
||
|
|
import bot
|
||
|
|
# The bot.py should handle its own execution
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ Failed to start Discord bot: {e}")
|
||
|
|
finally:
|
||
|
|
# Restore original working directory
|
||
|
|
if 'original_cwd' in locals():
|
||
|
|
os.chdir(original_cwd)
|
||
|
|
|
||
|
|
def signal_handler(signum, frame):
|
||
|
|
"""Handle shutdown signals"""
|
||
|
|
print("\n🛑 Shutting down...")
|
||
|
|
os._exit(0)
|
||
|
|
|
||
|
|
def main():
|
||
|
|
"""Main launcher function"""
|
||
|
|
print("🚀 Delta Bot Launcher")
|
||
|
|
print("=" * 30)
|
||
|
|
|
||
|
|
# Set up signal handlers for graceful shutdown
|
||
|
|
signal.signal(signal.SIGINT, signal_handler)
|
||
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
||
|
|
|
||
|
|
# Check if web UI should be disabled
|
||
|
|
disable_web = os.getenv('DISABLE_WEB_UI', 'false').lower() == 'true'
|
||
|
|
|
||
|
|
if not disable_web:
|
||
|
|
# Start web UI in a separate thread
|
||
|
|
web_thread = threading.Thread(target=start_web_ui, daemon=True)
|
||
|
|
web_thread.start()
|
||
|
|
|
||
|
|
# Give web UI time to start and display URLs
|
||
|
|
time.sleep(3)
|
||
|
|
else:
|
||
|
|
print("🚫 Web UI disabled by DISABLE_WEB_UI environment variable")
|
||
|
|
|
||
|
|
# Start Discord bot (this will block)
|
||
|
|
try:
|
||
|
|
start_discord_bot()
|
||
|
|
except KeyboardInterrupt:
|
||
|
|
print("\n🛑 Received interrupt signal")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"❌ Fatal error: {e}")
|
||
|
|
finally:
|
||
|
|
print("👋 Goodbye!")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|