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.
This commit is contained in:
parent
16355e93a2
commit
0fa15014d8
14
commands.py
Normal file
14
commands.py
Normal file
@ -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.")
|
60
main.py
Normal file
60
main.py
Normal file
@ -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.")
|
Loading…
x
Reference in New Issue
Block a user