-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
93 lines (74 loc) · 2.69 KB
/
bot.py
File metadata and controls
93 lines (74 loc) · 2.69 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
"""
Buchclub Discord Bot — Einstiegspunkt
Startet den Bot, lädt alle Cogs und registriert Slash Commands.
"""
import asyncio
import os
import logging
import discord
from discord.ext import commands
from dotenv import load_dotenv
from database import Database
# Logging einrichten
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log = logging.getLogger("buchclub")
# .env laden
load_dotenv()
# Intents — welche Discord-Events der Bot empfangen darf
intents = discord.Intents.default()
intents.message_content = True # Für zukünftige Prefix-Erweiterungen
intents.members = True
class BuchclubBot(commands.Bot):
"""Haupt-Bot-Klasse mit Lebenszyklus-Management."""
def __init__(self):
super().__init__(
command_prefix="!", # Fallback, wir nutzen primär Slash Commands
intents=intents,
help_command=None,
)
self.db: Database | None = None
async def setup_hook(self) -> None:
"""Wird einmalig beim Start aufgerufen — Datenbank + Cogs laden."""
# Datenbank initialisieren
self.db = Database(os.getenv("DB_PATH", "/data/buchclub.db"))
await self.db.setup()
log.info("Datenbank initialisiert.")
# Cogs laden
await self.load_extension("cogs.books")
await self.load_extension("cogs.progress")
await self.load_extension("cogs.profiles")
log.info("Cogs geladen.")
# Slash Commands synchronisieren
guild_id = os.getenv("DISCORD_GUILD_ID")
if guild_id:
# Entwicklungsmodus: nur auf einem Server (sofort aktiv)
guild = discord.Object(id=int(guild_id))
self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)
log.info(f"Slash Commands auf Guild {guild_id} synchronisiert.")
else:
# Produktivmodus: global (bis zu 1h Verzögerung)
await self.tree.sync()
log.info("Slash Commands global synchronisiert.")
async def on_ready(self) -> None:
"""Wird aufgerufen wenn der Bot verbunden und bereit ist."""
log.info(f"✅ Bot bereit als {self.user} (ID: {self.user.id})")
await self.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name="Die Buchketiere 📚",
)
)
async def main() -> None:
token = os.getenv("DISCORD_TOKEN")
if not token:
raise ValueError("DISCORD_TOKEN nicht in .env gesetzt!")
bot = BuchclubBot()
async with bot:
await bot.start(token)
if __name__ == "__main__":
asyncio.run(main())