110 lines
3.0 KiB
Python
110 lines
3.0 KiB
Python
# main.py
|
|
import discord
|
|
from discord.ext import commands
|
|
from config import ALLOWED_CHANNEL_ID, HIT_CHECK_INTERVAL, REASSIGN_DELAY, DISCORD_TOKEN
|
|
from cogs.assignments import Assignments
|
|
from cogs.commands import HitCommands
|
|
|
|
import asyncio
|
|
import uvicorn
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from services.bot_assignment import BotAssignmentManager
|
|
|
|
# Import routers
|
|
from routers import pages, factions, assignments, discord_mappings, config
|
|
from routers import bot as bot_router
|
|
|
|
|
|
# FastAPI Setup
|
|
app = FastAPI()
|
|
|
|
app.mount("/static", StaticFiles(directory="static"), name="static")
|
|
|
|
# Include all routers
|
|
app.include_router(pages.router)
|
|
app.include_router(factions.router)
|
|
app.include_router(assignments.router)
|
|
app.include_router(bot_router.router)
|
|
app.include_router(discord_mappings.router)
|
|
app.include_router(config.router)
|
|
|
|
|
|
# Discord Bot Setup
|
|
intents = discord.Intents.default()
|
|
intents.message_content = True
|
|
|
|
enrolled_attackers = []
|
|
enemy_queue = []
|
|
active_assignments = {}
|
|
round_robin_index = 0
|
|
|
|
class HitDispatchBot(commands.Bot):
|
|
async def setup_hook(self):
|
|
await self.add_cog(
|
|
Assignments(
|
|
self,
|
|
enemy_queue=enemy_queue,
|
|
active_assignments=active_assignments,
|
|
enrolled_attackers=enrolled_attackers,
|
|
hit_check=HIT_CHECK_INTERVAL,
|
|
reassign_delay=REASSIGN_DELAY,
|
|
)
|
|
)
|
|
|
|
await self.add_cog(
|
|
HitCommands(
|
|
self,
|
|
enrolled_attackers=enrolled_attackers,
|
|
enemy_queue=enemy_queue
|
|
)
|
|
)
|
|
|
|
async def cog_check(self, ctx):
|
|
return ctx.channel.id == ALLOWED_CHANNEL_ID
|
|
|
|
bot = HitDispatchBot(command_prefix="!", intents=intents)
|
|
|
|
# Initialize bot assignment manager
|
|
assignment_manager = None
|
|
|
|
@bot.event
|
|
async def on_ready():
|
|
global assignment_manager
|
|
print(f"Discord bot logged in as {bot.user.name} (ID: {bot.user.id})")
|
|
print(f"Bot is in {len(bot.guilds)} server(s)")
|
|
|
|
# Initialize assignment manager
|
|
assignment_manager = BotAssignmentManager(bot)
|
|
|
|
# Set assignment manager in routers
|
|
bot_router.set_assignment_manager(assignment_manager)
|
|
discord_mappings.set_assignment_manager(assignment_manager)
|
|
|
|
print("Bot assignment manager initialized")
|
|
|
|
async def start_bot():
|
|
try:
|
|
print("Starting Discord bot...")
|
|
await bot.start(DISCORD_TOKEN)
|
|
except discord.LoginFailure:
|
|
print("ERROR: Invalid Discord token! Please set DISCORD_TOKEN in config.py")
|
|
except Exception as e:
|
|
print(f"ERROR starting Discord bot: {e}")
|
|
|
|
# Main Entry Point
|
|
async def main():
|
|
# Start Discord bot in background
|
|
bot_task = asyncio.create_task(start_bot())
|
|
|
|
# Configure and run FastAPI server
|
|
config = uvicorn.Config(app, host="127.0.0.1", port=8000, log_level="info")
|
|
server = uvicorn.Server(config)
|
|
|
|
await server.serve()
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|