This repository was archived by the owner on Jan 20, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (66 loc) · 2.26 KB
/
main.py
File metadata and controls
86 lines (66 loc) · 2.26 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
import os
import asyncio
import discord
from discord.ext import commands
from dotenv import load_dotenv
# ---------- ENV ----------
def configure():
load_dotenv()
token = os.getenv("DISCORD_BOT_TOKEN")
channel_id = os.getenv("QUESTION_CHANNEL_ID")
if not token:
raise RuntimeError("DISCORD_BOT_TOKEN is missing in .env")
if not channel_id or not channel_id.isdigit():
raise RuntimeError("QUESTION_CHANNEL_ID is missing or invalid in .env")
return token, int(channel_id)
# ---------- BOT ----------
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)
# ---------- EVENTS ----------
@bot.event
async def on_ready():
print(f"+> Logged in as {bot.user}")
try:
synced = await bot.tree.sync()
print(f"+> Synced {len(synced)} slash commands.")
except Exception as e:
print(f"Failed to sync commands: {e}")
# ---------- MAIN ----------
async def main():
token, question_channel_id = configure()
try:
# Datenbank und Cog laden
from database import init_db
await init_db()
from cogs.quiz_cog import setup as quiz_setup
await quiz_setup(bot, question_channel_id)
# CodeFlex Cog laden
from cogs.flex_cog import setup as flex_setup
await flex_setup(bot)
# Leaderboard Cog laden
from cogs.leaderboard_cog import setup as leaderboard_setup
await leaderboard_setup(bot)
# Help Cog laden
from cogs.help_cog import setup as help_setup
await help_setup(bot)
print("+> Starting bot...")
await bot.start(token)
except discord.ConnectionClosed as e:
print(f"❌ Discord connection closed unexpectedly: {e}")
except Exception as e:
print(f"❌ Unexpected error: {e}")
finally:
print("🔌 Cleaning up bot connection...")
await bot.close()
# ---------- ENTRY ----------
if __name__ == "__main__":
while True:
try:
asyncio.run(main())
except KeyboardInterrupt:
print("🛑 Bot manually stopped.")
break
except Exception as e:
print(f"🔁 Restarting after error: {e}")
asyncio.run(asyncio.sleep(10))