From 0fa15014d8a1b14af6740e9c89149f2ce8a514e9 Mon Sep 17 00:00:00 2001 From: Dan Date: Mon, 13 Jan 2025 21:32:31 -0500 Subject: [PATCH] Main.py - Functional source of the bot, handles all the discord bot functionality Commands.py - Allows the commands to be offloaded into another file for easy of edit/registering without overloading the main core. --- commands.py | 14 +++++++++++++ main.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 commands.py create mode 100644 main.py diff --git a/commands.py b/commands.py new file mode 100644 index 0000000..89ea038 --- /dev/null +++ b/commands.py @@ -0,0 +1,14 @@ +import discord + + +# Function to register all commands to the CommandTree +def register_commands(tree: discord.app_commands.CommandTree, guild: discord.Object = None): + # Ping Command + @tree.command(name="ping", description="Check if Amber is responsive.", guild=guild) + async def ping(interaction: discord.Interaction): + await interaction.response.send_message(f"Pong! Latency: {round(interaction.client.latency * 1000)}ms") + + # Example Command + @tree.command(name="hello", description="Say hello to Amber.", guild=guild) + async def hello(interaction: discord.Interaction): + await interaction.response.send_message("Hello! Amber here to help.") diff --git a/main.py b/main.py new file mode 100644 index 0000000..f75bb9d --- /dev/null +++ b/main.py @@ -0,0 +1,60 @@ +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.")