Files
faction_war_dispatch_bot/main.py

126 lines
3.7 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, auth, activity
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(auth.router)
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)
app.include_router(activity.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():
# Parse command-line arguments
import argparse
import os
# Read from environment variables first, then fall back to defaults
default_port = int(os.getenv("PORT", "8000"))
default_host = os.getenv("HOST", "127.0.0.1")
parser = argparse.ArgumentParser(description="Faction War Dispatch Bot")
parser.add_argument("--port", type=int, default=default_port, help=f"Port to run the application on (default: {default_port})")
parser.add_argument("--host", type=str, default=default_host, help=f"Host to bind to (default: {default_host})")
args = parser.parse_args()
# Start Discord bot in background
bot_task = asyncio.create_task(start_bot())
# Configure and run FastAPI server
config = uvicorn.Config(app, host=args.host, port=args.port, log_level="info")
server = uvicorn.Server(config)
print(f"Starting server on {args.host}:{args.port}")
await server.serve()
if __name__ == "__main__":
asyncio.run(main())