discord.py
discord.py copied to clipboard
Bot will not sync command tree
Summary
using the client.tree.sync or just tree.sync will not sync the app_commands in any guild
Reproduction Steps
You can define the bot/client as either of these two:
client = commands.Bot(command_prefix="!", intents=discord.Intents.all())
# OR
client = discord.Client(intents=discord.Intents.all())
tree = app_commands.CommandTree(client)
I put my guildIds in a list: guild_ids = [MY_GUILD_ID]
Then you have two options for automatically syncing:
- Create a setup_hook:
async def setup_hook() -> None:
client.tree.copy_global_to(guild=discord.Object(id=guild_ids[0])) # Optional if you don't want global?
await client.tree.sync(guild=discord.Object(id=guild_ids[0]))
client.setup_hook = setup_hook
- Sync on_ready:
@client.event
async def on_ready() -> None:
await client.tree.sync(guild=discord.Object(id=guild_ids[0]))
print(f"Logged in as {client.user}")
There is also the option to create a sync command:
@client.tree.command(name = "sync", description="Syncs the bot commands", guild=discord.Object(id=guild_ids[0])) # or just use @tree.command
async def sync(interaction: discord.Interaction):
if interaction.user.id == MY_USER_ID:
#client.tree.copy_global_to(guild=discord.Object(id=guild_ids[0])) # I tried putting it here too.
await client.tree.sync(guild=discord.Object(id=guild_ids[0]))
await interaction.response.send_message('Command tree synced.')
else:
await interaction.response.send('You must be the owner to use this command!')
Make sure the bot has application.command
permissions on the server
Minimal Reproducible Code
No response
Expected Results
I expected the commands to show up in discord.
Actual Results
The commands don't show up during the majority of my attempts. I think in one variation it synced once, but never again even after reproducing it. I have two commands registered and they have not synced since.
Intents
Intents.all()
System Information
discord.py==2.3.2 via requirements.txt (used in docker container)
Checklist
- [X] I have searched the open issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have removed my token from display, if visible.
Additional Context
No response
This is not an issue with discord.py.
The reason why is that the command is only synced to the guild, but not globally so it only shows up the commands that are marked to synced to that guild using @app_commands.guilds()
Some additional notes that I want to point out:
- It's recommended to not use the
on_ready
event to sync due to the dangers of the event being called randomly multiple times without warning. - you can't sync app commands using an app command when the app command itself is not synced. Instead, use a prefixed command instead (mentions for the bot can also work)
- subclass
setup_hook
indiscord.Client
(and all subclasses) instead of trying to attach it as an attribute