from fastapi import APIRouter, HTTPException from models import BotConfig, VoiceChannelJoinRequest, VoiceChannelLeaveRequest from internal.bot_manager import DiscordBotManager from internal.logger import create_logger LOGGER = create_logger(__name__) # Function to create router def create_bot_router(bot_manager: DiscordBotManager): router = APIRouter() # 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("/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.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