Repository navigation
Expand file tree
/
Copy pathbot.py
More file actions
123 lines (100 loc) · 4.02 KB
/
Copy pathbot.py
File metadata and controls
123 lines (100 loc) · 4.02 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
"""
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, tasks
from dotenv import load_dotenv
from database import Database
from services.backup import backup_dir, run_backup
# 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.members = True # /buchketiere zeigt die Anzeigenamen der Mitglieder
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")
await self.load_extension("cogs.goals")
await self.load_extension("cogs.achievements")
log.info("Cogs geladen.")
if backup_dir():
self.backup_loop.start()
log.info(f"Tägliche Sicherung nach {backup_dir()} eingerichtet.")
else:
log.warning("BACKUP_DIR ist nicht gesetzt: es gibt keine Sicherung der Datenbank.")
# 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.")
@tasks.loop(hours=24)
async def backup_loop(self) -> None:
try:
await run_backup(self.db)
except Exception:
log.exception("Sicherung fehlgeschlagen.")
async def close(self) -> None:
"""Beim Herunterfahren die Datenbank sauber schließen."""
if self.backup_loop.is_running():
self.backup_loop.cancel()
if self.db:
await self.db.close()
await super().close()
async def on_interaction(self, interaction: discord.Interaction) -> None:
"""Wie spät ein Befehl beim Bot ankommt. Discord wartet nur drei Sekunden auf die erste Antwort."""
lag = (discord.utils.utcnow() - interaction.created_at).total_seconds()
if lag > 1.5:
name = (interaction.data or {}).get("name") or (interaction.data or {}).get("custom_id") or interaction.type.name
log.warning(f"Interaktion '{name}' kam {lag:.1f} s nach dem Klick an.")
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())