Files
.gitignore
bot.py
community.py
config.json
config.py
database.py
embeds.py
general.py
moderation.py
README.md
requirements.txt
tickets.py
youtube.py
Code
13 939
bot.py
from __future__ import annotations
import logging
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
from community import CommunityCog
from config import Settings, load_settings
from database import Database
from general import GeneralCog
from moderation import ModerationCog
from tickets import TicketsCog
from youtube import YouTubeCog
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
LOGGER = logging.getLogger("zenix")
class ZenixBot(commands.Bot):
def __init__(self, settings: Settings) -> None:
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
super().__init__(command_prefix=commands.when_mentioned_or("!"), intents=intents, help_command=None)
self.settings = settings
self.database = Database(settings.database_path)
async def setup_hook(self) -> None:
await self.database.connect()
await self.add_cog(GeneralCog(self.settings))
await self.add_cog(CommunityCog(self.settings))
await self.add_cog(ModerationCog(self, self.settings, self.database))
await self.add_cog(TicketsCog(self.settings))
await self.add_cog(YouTubeCog(self, self.settings, self.database))
if self.settings.sync_guild_id:
guild = discord.Object(id=self.settings.sync_guild_id)
self.tree.copy_global_to(guild=guild)
await self.tree.sync(guild=guild)
LOGGER.info("Slash commands synced to guild %s", self.settings.sync_guild_id)
else:
await self.tree.sync()
LOGGER.info("Global slash commands synced")
async def close(self) -> None:
await self.database.close()
await super().close()
async def on_ready(self) -> None:
if self.user:
LOGGER.info("ZENIX online as %s (%s)", self.user, self.user.id)
async def on_app_command_error(self, interaction: discord.Interaction, error: app_commands.AppCommandError) -> None:
if isinstance(error, app_commands.CommandOnCooldown):
message = f"Bitte warte noch {error.retry_after:.1f} Sekunden."
elif isinstance(error, app_commands.MissingPermissions):
message = "Dir fehlen die notwendigen Berechtigungen."
else:
LOGGER.error("Slash command error", exc_info=error)
message = "Bei der Ausführung ist ein Fehler aufgetreten."
if interaction.response.is_done():
await interaction.followup.send(message, ephemeral=True)
else:
await interaction.response.send_message(message, ephemeral=True)
def main() -> None:
load_dotenv()
settings = load_settings()
if not settings.token or settings.token == "replace_with_your_bot_token":
raise RuntimeError("DISCORD_TOKEN fehlt. Lege ihn in einer .env-Datei fest.")
bot = ZenixBot(settings)
bot.run(settings.token)
if __name__ == "__main__":
main()