16dvnk commited on
Commit
2742de6
Β·
1 Parent(s): f06bff1
Files changed (2) hide show
  1. app.py +2 -2
  2. bot_manager.py +8 -2
app.py CHANGED
@@ -39,9 +39,9 @@ with app.app_context():
39
  except Exception as e:
40
  print(f"Could not load environment variables from database: {e}")
41
 
42
- # Register blueprints
43
  from routes.dashboard import dashboard_bp
44
  from routes.api import api_bp
45
 
46
  app.register_blueprint(dashboard_bp)
47
- app.register_blueprint(api_bp, url_prefix='/api')
 
39
  except Exception as e:
40
  print(f"Could not load environment variables from database: {e}")
41
 
42
+ # βœ… Register blueprints (moved below app context to avoid circular import)
43
  from routes.dashboard import dashboard_bp
44
  from routes.api import api_bp
45
 
46
  app.register_blueprint(dashboard_bp)
47
+ app.register_blueprint(api_bp, url_prefix='/api')
bot_manager.py CHANGED
@@ -2,7 +2,7 @@ import asyncio
2
  import threading
3
  import os
4
  from datetime import datetime
5
- from app import app, db
6
  from models import BotStatus, BotLog
7
  from discord_bot import run_bot
8
 
@@ -20,6 +20,7 @@ class BotManager:
20
 
21
  def log_callback(self, level, message):
22
  """Callback function to handle bot logs"""
 
23
  with app.app_context():
24
  try:
25
  log_entry = BotLog(level=level, message=message)
@@ -38,6 +39,7 @@ class BotManager:
38
 
39
  def _run_bot_in_thread(self):
40
  """Run the bot in a separate thread with its own event loop"""
 
41
  try:
42
  self.loop = asyncio.new_event_loop()
43
  asyncio.set_event_loop(self.loop)
@@ -70,6 +72,7 @@ class BotManager:
70
 
71
  def start_bot(self):
72
  """Start the Discord bot in a separate thread"""
 
73
  if self.is_running:
74
  return False, "Bot is already running"
75
 
@@ -97,6 +100,7 @@ class BotManager:
97
 
98
  def stop_bot(self):
99
  """Stop the Discord bot"""
 
100
  if not self.is_running:
101
  return False, "Bot is not running"
102
 
@@ -148,6 +152,7 @@ class BotManager:
148
 
149
  def _sync_with_database(self):
150
  """Sync bot manager state with database on startup"""
 
151
  try:
152
  with app.app_context():
153
  status = BotStatus.get_current_status()
@@ -162,6 +167,7 @@ class BotManager:
162
 
163
  def get_status(self):
164
  """Get current bot status"""
 
165
  with app.app_context():
166
  status = BotStatus.get_current_status()
167
  return {
@@ -171,4 +177,4 @@ class BotManager:
171
  }
172
 
173
  # Global bot manager instance
174
- bot_manager = BotManager()
 
2
  import threading
3
  import os
4
  from datetime import datetime
5
+ from extensions import db # βœ… Safe import
6
  from models import BotStatus, BotLog
7
  from discord_bot import run_bot
8
 
 
20
 
21
  def log_callback(self, level, message):
22
  """Callback function to handle bot logs"""
23
+ from app import app # πŸ” Lazy import to avoid circular dependency
24
  with app.app_context():
25
  try:
26
  log_entry = BotLog(level=level, message=message)
 
39
 
40
  def _run_bot_in_thread(self):
41
  """Run the bot in a separate thread with its own event loop"""
42
+ from app import app # πŸ” Lazy import
43
  try:
44
  self.loop = asyncio.new_event_loop()
45
  asyncio.set_event_loop(self.loop)
 
72
 
73
  def start_bot(self):
74
  """Start the Discord bot in a separate thread"""
75
+ from app import app # πŸ” Lazy import
76
  if self.is_running:
77
  return False, "Bot is already running"
78
 
 
100
 
101
  def stop_bot(self):
102
  """Stop the Discord bot"""
103
+ from app import app # πŸ” Lazy import
104
  if not self.is_running:
105
  return False, "Bot is not running"
106
 
 
152
 
153
  def _sync_with_database(self):
154
  """Sync bot manager state with database on startup"""
155
+ from app import app # πŸ” Lazy import
156
  try:
157
  with app.app_context():
158
  status = BotStatus.get_current_status()
 
167
 
168
  def get_status(self):
169
  """Get current bot status"""
170
+ from app import app # πŸ” Lazy import
171
  with app.app_context():
172
  status = BotStatus.get_current_status()
173
  return {
 
177
  }
178
 
179
  # Global bot manager instance
180
+ bot_manager = BotManager()