Lazo Lab Sign up

App 8: Build a Discord Bot / Build a Discord bot

Lesson 5 of 10

Slash commands

Slash commands like /hello show up in Discord's menu. A CommandTree holds them; sync it once the bot is ready.

Key points

  • app_commands.CommandTree(client)
  • @tree.command(...)
  • await tree.sync() registers them
from discord import app_commands

tree = app_commands.CommandTree(client)

@tree.command(name="hello", description="Say hello")
async def hello(interaction: discord.Interaction):
    await interaction.response.send_message(f"Salam, {interaction.user.display_name}! 👋")

@client.event
async def on_ready():
    await tree.sync()
    print("Commands synced")

Your turn

Add /hello and try it in your server. New commands can take a minute to appear.

Watch a video on thisOpens YouTube search results for “Slash commands” in a new tab

Quiz · +10 XP

What does await tree.sync() do?

Log in to save progress and earn XP.