Discord bot using Python with discord.app_commands.CommandTree:
-
Install the required libraries:
pip install discord.py
-
Create the bot:
import discord from discord.ext import commands from discord import app_commands intents = discord.Intents.all() bot = commands.Bot(command_prefix='!', intents=intents, status=discord.Status.do_not_disturb, activity=discord.Game(name='Playing with Integration')) @bot.event async def on_ready(): await bot.tree.sync() print(f'Logged in as {bot.user}!') bot.run('YOUR_BOT_TOKEN')
-
Create a simple slash command:
@bot.event async def on_ready(): await bot.tree.sync() print(f'Logged in as {bot.user}!') @bot.tree.command(name="hello") async def hello(interaction: discord.Interaction): await interaction.response.send_message("Hello! How can I assist you today?")
-
Command with options:
@bot.tree.command(name="greet") @app_commands.describe(name="The name of the person to greet") async def greet(interaction: discord.Interaction, name: str): await interaction.response.send_message(f"Hello, {name}!")
-
Command with choices:
@bot.tree.command(name="choose") @app_commands.choices(color=[ app_commands.Choice(name="Red", value=1), app_commands.Choice(name="Green", value=2), app_commands.Choice(name="Blue", value=3), ]) async def choose(interaction: discord.Interaction, color: app_commands.Choice[int]): await interaction.response.send_message(f"You chose {color.name}!")
-
Command with subcommands:
class MyGroup(app_commands.Group): @app_commands.command(name="subcommand1") async def subcommand1(self, interaction: discord.Interaction): await interaction.response.send_message("This is subcommand 1") @app_commands.command(name="subcommand2") async def subcommand2(self, interaction: discord.Interaction): await interaction.response.send_message("This is subcommand 2") bot.tree.add_command(MyGroup(name="group", description="A group of commands"))
-
Handling errors:
@bot.tree.error async def on_app_command_error(interaction: discord.Interaction, error: app_commands.AppCommandError): await interaction.response.send_message(f"An error occurred: {error}", ephemeral=True)
To ensure that the commands are registered and updated properly:
@bot.event
async def on_ready():
await bot.tree.sync()
print(f'Logged in as {bot.user}!')Make sure you have your bot token and run your script:
bot.run('YOUR_BOT_TOKEN')-
Decorator for Command Registration:
@bot.tree.command(name="greet")@bot.tree.command: This decorator registers a new slash command.name="greet": Specifies the name of the slash command. This is the command users will type in the chat (e.g.,/greet).
-
Decorator to Describe the Command's Options:
@app_commands.describe(name="The name of the person to greet")@app_commands.describe: This decorator provides a description for the command's parameters.name="The name of the person to greet": Describes thenameparameter, making it clear to users what they should provide.
-
Async Function Definition:
async def greet(interaction: discord.Interaction, name: str):
async def greet: Defines an asynchronous function namedgreet. The function will be executed when the slash command is invoked.interaction: discord.Interaction: Represents the interaction with the Discord API when a user invokes the command. This object provides context and methods to respond to the command.name: str: A string parameter that will be provided by the user when invoking the command. This is the name of the person to greet.
-
Sending a Response:
await interaction.response.send_message(f"Hello, {name}!")
await interaction.response.send_message: Sends a message in response to the interaction.f"Hello, {name}!": A formatted string that includes thenameprovided by the user, resulting in a personalized greeting message.
Here’s the complete code snippet with all components explained:
@bot.tree.command(name="greet")
@app_commands.describe(name="The name of the person to greet")
async def greet(interaction: discord.Interaction, name: str):
await interaction.response.send_message(f"Hello, {name}!")- @bot.tree.command(name="greet"): Registers the command
/greet. - @app_commands.describe(name="The name of the person to greet"): Describes the
nameparameter for user clarity. - async def greet(interaction: discord.Interaction, name: str): Defines the function to handle the command.
- await interaction.response.send_message(f"Hello, {name}!"): Sends a personalized greeting as a response to the command.