import discord import os from dotenv import load_dotenv from commands import register_commands # Import the command registration function # Load environment variables from .env load_dotenv() # Get the bot token from the .env file TOKEN = os.getenv("DISCORD_TOKEN") # Define whether to sync globally or for a specific guild DEF_PARAM = "GUILD" # Options: "GLOBAL" or "GUILD" # Set the guild ID if using guild-specific sync GUILD_ID = os.getenv("GUILD_ID") class AmberClient(discord.Client): def __init__(self): intents = discord.Intents.default() intents.guilds = True # Required for guild interactions super().__init__(intents=intents) # Command tree for slash commands self.tree = discord.app_commands.CommandTree(self) async def setup_hook(self): # Register all commands from commands.py if DEF_PARAM == "GUILD" and GUILD_ID: guild = discord.Object(id=int(GUILD_ID)) register_commands(self.tree, guild=guild) # Register with specific guild else: register_commands(self.tree) # Register globally # Sync commands after registering try: if DEF_PARAM == "GLOBAL": # Sync globally synced = await self.tree.sync() print(f"Synced {len(synced)} commands globally!") elif DEF_PARAM == "GUILD" and GUILD_ID: # Sync to a specific guild guild = discord.Object(id=int(GUILD_ID)) synced = await self.tree.sync(guild=guild) print(f"Synced {len(synced)} commands to guild {GUILD_ID}!") else: print("Error: DEF_PARAM is set to 'GUILD' but GUILD_ID is not provided.") except Exception as e: print(f"Failed to sync commands: {e}") # Create client instance amber = AmberClient() # Run the bot if TOKEN: amber.run(TOKEN) else: print("Error: DISCORD_TOKEN is not set in the .env file.")