-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
195 lines (161 loc) · 5.99 KB
/
main.py
File metadata and controls
195 lines (161 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
"""
ManagerX Discord Bot - Main Entry Point
========================================
Copyright (c) 2025 OPPRO.NET Network
Version: 2.0.0
"""
# =============================================================================
# IMPORTS
# =============================================================================
import discord
import sys
import asyncio
from pathlib import Path
from datetime import datetime
from colorama import Fore, Style, init as colorama_init
from dotenv import load_dotenv
import ezcord
from ezcord import CogLog
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from uvicorn import Server, Config
# Logger (muss existieren!)
from logger import logger
# =============================================================================
# SETUP
# =============================================================================
BASEDIR = Path(__file__).resolve().parent
load_dotenv(dotenv_path=BASEDIR / 'config' / '.env')
# Lokale Module aus src/bot/core
from src.bot.core.config import ConfigLoader, BotConfig
from src.bot.core.bot_setup import BotSetup
from src.bot.core.cog_manager import CogManager
from src.bot.core.database import DatabaseManager
from src.bot.core.dashboard import DashboardTask
from src.bot.core.utils import print_logo
# API Routes für Dashboard
from src.api.dashboard.routes import set_bot_instance, router as dashboard_router
from mx_handler import TranslationHandler
colorama_init(autoreset=True)
TranslationHandler.settings(
path="translation/messages",
default_lang="de",
fallback_langs=("en", "de"),
logging=False,
colored=False,
log_level="DEBUG"
)
# Sys-Path
if str(BASEDIR) not in sys.path:
sys.path.append(str(BASEDIR))
# =============================================================================
# FASTAPI SETUP
# =============================================================================
app = FastAPI(
title="ManagerX Dashboard API",
description="Live Bot Status & Statistiken API",
version=BotConfig.VERSION
)
# CORS aktivieren
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Dashboard-Routes einbinden
app.include_router(dashboard_router)
async def start_webserver():
"""Startet den FastAPI Webserver auf Port 8040"""
config = Config(app=app, host="0.0.0.0", port=8040, log_level="error")
server = Server(config)
await server.serve()
logger.success("API", "FastAPI-Server läuft auf http://0.0.0.0:8040")
# =============================================================================
# MAIN EXECUTION
# =============================================================================
if __name__ == '__main__':
# Logo ausgeben
print_logo()
# Konfiguration laden
logger.info("BOT", "Lade Konfiguration...")
config_loader = ConfigLoader(BASEDIR)
config = config_loader.load()
logger.success("BOT", "Konfiguration geladen")
# Bot erstellen
logger.info("BOT", "Initialisiere Bot...")
bot_setup = BotSetup(config)
bot = bot_setup.create_bot()
# Speichere Bot Start-Zeit für Uptime-Berechnung
bot.start_time = datetime.utcnow()
# Übergebe Bot-Instanz an die API-Routes
set_bot_instance(bot)
logger.info("API", "Bot-Instanz an Dashboard-API übergeben")
# Datenbank initialisieren
db_manager = DatabaseManager()
if not db_manager.initialize(bot):
logger.warn("DATABASE", "Bot läuft ohne Datenbank weiter...")
else:
logger.success("DATABASE", "Datenbank erfolgreich initialisiert")
# Dashboard-Task registrieren
dashboard = DashboardTask(bot, BASEDIR)
dashboard.register()
@bot.event
async def on_ready():
logger.success("BOT", f"Logged in as {bot.user.name}")
# --- NEU: Status API & Webserver starten ---
bot.loop.create_task(start_webserver())
# Dashboard starten
dashboard.start()
# Bot-Status
if config['features'].get('bot_status', True):
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name=f"ManagerX v{BotConfig.VERSION}"
)
)
# Commands sync
await bot.sync_commands()
logger.success("COMMANDS", "Application Commands synchronisiert")
# --- LIMIT CHECK START ---
all_cmds = bot.pending_application_commands
# Wir zählen nur die echten Top-Level Slash Commands (Slots)
root_slots = [c for c in all_cmds if isinstance(c, discord.SlashCommand)]
logger.info("LIMITS", f"EzCord zählt (alle Funktionen): {len(bot.commands)}")
logger.info("LIMITS", f"Discord-API Slots belegt: {len(root_slots)} / 100")
# --- LIMIT CHECK ENDE ---
# Minimaler KeepAlive Cog
class KeepAlive(discord.ext.commands.Cog):
def __init__(self, bot):
self.bot = bot
@discord.ext.commands.Cog.listener()
async def on_ready(self):
logger.info("KEEPALIVE", "KeepAlive Cog aktiv - Bot bleibt online")
bot.add_cog(KeepAlive(bot))
logger.success("BOT", "KeepAlive Cog geladen")
# Cogs laden
logger.info("BOT", "Lade Cogs...")
cog_manager = CogManager(config['cogs'])
ignored = cog_manager.get_ignored_cogs()
bot.load_cogs(
"src/bot/cogs",
subdirectories=True,
ignored_cogs=ignored,
)
logger.success("BOT", "Cogs geladen")
# Token prüfen
if not BotConfig.TOKEN:
logger.critical("DEBUG", "Kein TOKEN in .env gefunden!")
sys.exit(1)
# Bot starten
logger.info("BOT", "Starte Bot...")
try:
bot.run(BotConfig.TOKEN)
except discord.LoginFailure:
logger.critical("BOT", "Ungültiger Token!")
sys.exit(1)
except Exception as e:
logger.critical("BOT", f"Bot-Start fehlgeschlagen: {e}")
sys.exit(1)