Код: Выделить всё
# Function to register commands
def register_commands(client):
print("Starting command registration...")
@client.tree.command(name="language", description="Select your language")
async def language_command(interaction: discord.Interaction):
print("Language command registered")
await _language(client, interaction)
@client.tree.command(name="reactions", description="Set up reaction roles")
async def reactions_command(interaction: discord.Interaction):
print("Reactions command registered")
await _reactions(client, interaction)
@client.tree.command(name="analyticsmessage", description="Fetch all messages from a specific user")
async def analyticsmessage_command(interaction: discord.Interaction):
print("AnalyticsMessage command registered")
await _analyticsmessage(client, interaction)
# Command to manually sync commands
@client.tree.command(name="sync", description="Manually sync commands")
@app_commands.checks.has_permissions(administrator=True)
async def sync_command(interaction: discord.Interaction):
await client.tree.sync()
await interaction.response.send_message("Commands synced.", ephemeral=True)
print("Commands manually synced")
print("Commands registration complete")
Код: Выделить всё
import discord
from discord.ext import commands
import os
from dotenv import load_dotenv
from config import load_config
from events import register_event_handlers
from commands import register_commands
# Load environment variables from .env file
load_dotenv()
# Get the necessary configurations from environment variables
TOKEN = os.getenv('TOKEN')
# Set up intents for the bot to access guild, member, and message information
intents = discord.Intents.default()
intents.members = True
intents.guilds = True
intents.messages = True
intents.message_content = True
# Custom bot client class
class MyClient(commands.Bot):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.config = load_config()
async def setup_hook(self):
# Register commands
print("Registering commands...")
register_commands(self)
# Ensure commands are registered globally initially
await self.tree.sync(guild=None)
print("Commands synced globally")
# Initialize the bot client
client = MyClient(command_prefix='!', intents=intents)
# Register event handlers
register_event_handlers(client)
# Run the bot
client.run(TOKEN)
Код: Выделить всё
Registering commands... Starting command registration... Commands registration complete Commands synced globally
Подробнее здесь: https://stackoverflow.com/questions/786 ... mmand-tree