Repository: Rapptz/discord.py
File: examples/app_commands/basic.py (Python)

Code before:
import discord
from discord import app_commands
from discord.ext import commands

class MyBot(commands.Bot):

    # In this basic example, we just synchronize the app commands to one guild,
    # without requiring to set the guild to every command individually by just copying them over.
    # By doing so we don't have to wait up to 1hr until they are shown for the end-user.

    # If you want to synchronize your app commands to every guild your bot is in, 
    # remove the following setup_hook and uncomment the one below.
    async def setup_hook(self):
        # Replace ... with the guild id you want your commands to be synced with.
        guild = discord.Object(...)
        self.tree.copy_global_to(guild=guild)
        synced_app_commands = await self.tree.sync(guild=guild)
        print(f'Synced the following commands to {guild.id}: {[c.name for c in synced_app_commands]}')

    # async def setup_hook(self):
    #     synced_app_commands = await self.tree.sync()
    #     print(f'Synced the following commands to every guild: {[c.name for c in synced_app_commands]}')

intents = discord.Intents.default()

# In order to use a basic synchronization of the app commands in the setup_hook, 
# you have replace ... with your bots application_id you find in the developer portal.
bot = MyBot(command_prefix='?', intents=intents, application_id=...)


@bot.event
async def on_ready():
    print(f'Logged in as {bot.user} (ID: {bot.user.id})')
    print('------')

@bot.tree.command()
async def hello(interaction: discord.Interaction):
    """Says hello!"""
    await interaction.response.send_message(f'Hi, {interaction.user.mention}')

@bot.tree.command()
@app_commands.describe(first_value='The first value you want to add something to', second_value='The value you want to add to the first value')
async def add(interaction: discord.Interaction, first_value: int, second_value: int):

Review comment (suggestion):
I don't think there's much of a point to make use of the output here. Might as well leave it as a bare sync.

Code after:
from typing import Optional

import discord
from discord import app_commands

MY_GUILD = discord.Object(id=0)  # replace with your guild id


class MyClient(discord.Client):
    def __init__(self, *, intents: discord.Intents, application_id: int):
        super().__init__(intents=intents, application_id=application_id)
        # A CommandTree is a special type that holds all the application command
        # state required to make it work. This is a separate class because it
        # allows all the extra state to be opt-in.
        # Whenever you want to work with application commands, your tree is used
        # to store it and work with it.
        # Note: When using commands.Bot instead of discord.Client, the bot will
        # maintain its own tree instead.
        self.tree = app_commands.CommandTree(self)

    # In this basic example, we just synchronize the app commands to one guild.
    # Instead of specifying a guild to every command, we copy over our global commands instead.
    # By doing so we don't have to wait up to an hour until they are shown to the end-user.
    async def setup_hook(self):
        # This copies the global commands over to your guild.
        self.tree.copy_global_to(guild=MY_GUILD)
        await self.tree.sync(guild=MY_GUILD)


intents = discord.Intents.default()

# In order to use a basic synchronization of the app commands in the setup_hook,
# you have replace the 0 with your bots application_id you find in the developer portal.
client = MyClient(intents=intents, application_id=0)


@client.event
async def on_ready():
    print(f'Logged in as {client.user} (ID: {client.user.id})')
    print('------')


@client.tree.command()