Refactored to better split everything up
This commit is contained in:
62
app/routers/bot.py
Normal file
62
app/routers/bot.py
Normal file
@@ -0,0 +1,62 @@
|
||||
import asyncio
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from typing import Optional, Dict
|
||||
from models import BotConfig, VoiceChannelRequest
|
||||
from internal.bot_manager import DiscordBotManager
|
||||
from internal.logger import create_logger
|
||||
|
||||
LOGGER = create_logger(__name__)
|
||||
|
||||
# Define FastAPI app
|
||||
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))
|
||||
|
||||
@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: VoiceChannelRequest):
|
||||
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: VoiceChannelRequest):
|
||||
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())
|
||||
}
|
||||
return status
|
||||
Reference in New Issue
Block a user