Quartessa/main.py

82 lines
2.7 KiB
Python

import os
import logging
import discord
import subprocess
from dotenv import load_dotenv
from commands import setup_commands
# Load environment variables
load_dotenv()
TOKEN = os.getenv("DISCORD_TOKEN")
GUILD_ID = os.getenv("GUILD_ID")
DEFAULT_OWNER_ID = os.getenv("DEFAULT_OWNER_ID")
# Validate Guild ID
if GUILD_ID:
try:
GUILD_ID = int(GUILD_ID)
except ValueError:
logging.error("Invalid GUILD_ID in .env file. It must be a numeric value.")
GUILD_ID = None
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
intents.guilds = True
intents.voice_states = True
class AmberClient(discord.Client):
def __init__(self):
super().__init__(intents=intents)
self.tree = discord.app_commands.CommandTree(self)
async def on_ready(self):
try:
app_info = await client.application_info()
if hasattr(app_info, "team") and app_info.team: # Check if the bot is part of a team
team_owner_id = app_info.team.owner_id
owner = await client.fetch_user(team_owner_id)
print(f"✅ Team owner fetched successfully: {owner} ({team_owner_id})")
else:
owner = app_info.owner or await client.fetch_user(DEFAULT_OWNER_ID)
print(f"✅ Individual owner fetched successfully: {owner} ({owner.id})")
# Send startup notification
try:
commit_hash = subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("utf-8").strip()
startup_message = (
f"🚀 **Quartessa is online!**\n"
f"Version: `{commit_hash}`\n\n"
"Use `/check_update` to check for updates or `/update` to apply updates."
)
await owner.send(startup_message)
except discord.Forbidden:
print("⚠️ Unable to send startup notification to the owner. Check privacy settings.")
except Exception as e:
print(f"⚠️ Error fetching owner information: {e}")
# Sync commands after the bot is fully ready
if GUILD_ID:
logging.info(f"Setting up commands for guild: {GUILD_ID}")
else:
logging.info("Setting up global commands (no guild ID specified).")
try:
await setup_commands(self, guild_id=GUILD_ID)
except Exception as e:
logging.error(f"Failed to setup commands: {e}")
print(f"✅ Quartessa is online as {client.user}. Done.")
logging.basicConfig(level=logging.INFO)
client = AmberClient()
if TOKEN:
client.run(TOKEN)
else:
logging.error("Bot token not found. Check your .env file.")