This commit is contained in:
@@ -5,54 +5,53 @@ from internal.logger import create_logger
|
||||
|
||||
LOGGER = create_logger(__name__)
|
||||
|
||||
# Define FastAPI app
|
||||
router = APIRouter()
|
||||
def create_bot_router(bot_manager: DiscordBotManager): # Function to create router
|
||||
router = APIRouter()
|
||||
|
||||
# Initialize Discord Bot Manager
|
||||
bot_manager = DiscordBotManager()
|
||||
# API Endpoints
|
||||
@router.post("/start_bot")
|
||||
async def start_bot(config: BotConfig):
|
||||
try:
|
||||
await bot_manager.start_bot(config.token)
|
||||
return {"status": "Bot started successfully."}
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Error starting bot: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
# API Endpoints
|
||||
@router.post("/start_bot")
|
||||
async def start_bot(config: BotConfig):
|
||||
try:
|
||||
await bot_manager.start_bot(config.token)
|
||||
return {"status": "Bot started successfully."}
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Error starting bot: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
@router.post("/stop_bot")
|
||||
async def stop_bot():
|
||||
try:
|
||||
await bot_manager.stop_bot()
|
||||
return {"status": "Bot stopped successfully."}
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Error stopping bot: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.post("/stop_bot")
|
||||
async def stop_bot():
|
||||
try:
|
||||
await bot_manager.stop_bot()
|
||||
return {"status": "Bot stopped successfully."}
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Error stopping bot: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
@router.post("/join_voice")
|
||||
async def join_voice_channel(request: VoiceChannelJoinRequest):
|
||||
try:
|
||||
await bot_manager.join_voice_channel(request.guild_id, request.channel_id)
|
||||
return {"status": f"Joined guild {request.guild_id} voice channel {request.channel_id}."}
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Error joining voice channel: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.post("/join_voice")
|
||||
async def join_voice_channel(request: VoiceChannelJoinRequest):
|
||||
try:
|
||||
await bot_manager.join_voice_channel(request.guild_id, request.channel_id)
|
||||
return {"status": f"Joined guild {request.guild_id} voice channel {request.channel_id}."}
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Error joining voice channel: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
@router.post("/leave_voice")
|
||||
async def leave_voice_channel(request: VoiceChannelLeaveRequest):
|
||||
try:
|
||||
await bot_manager.leave_voice_channel(request.guild_id)
|
||||
return {"status": f"Left guild {request.guild_id} voice channel."}
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Error leaving voice channel: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.post("/leave_voice")
|
||||
async def leave_voice_channel(request: VoiceChannelLeaveRequest):
|
||||
try:
|
||||
await bot_manager.leave_voice_channel(request.guild_id)
|
||||
return {"status": f"Left guild {request.guild_id} voice channel."}
|
||||
except Exception as e:
|
||||
LOGGER.error(f"Error leaving voice channel: {e}")
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
|
||||
@router.get("/status")
|
||||
async def get_status():
|
||||
status = {
|
||||
"bot_running": bot_manager.bot is not None and not bot_manager.bot.is_closed(),
|
||||
"connected_guilds": list(bot_manager.voice_clients.keys()),
|
||||
"active_token": bot_manager.token
|
||||
}
|
||||
return status
|
||||
@router.get("/status")
|
||||
async def get_status():
|
||||
status = {
|
||||
"bot_running": bot_manager.bot is not None and not bot_manager.bot.is_closed(),
|
||||
"connected_guilds": list(bot_manager.voice_clients.keys()),
|
||||
"active_token": bot_manager.token
|
||||
}
|
||||
return status
|
||||
|
||||
return router # Return the configured router
|
||||
|
||||
Reference in New Issue
Block a user