Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot will not sync command tree #9717

Open
3 tasks done
lucasballr opened this issue Jan 24, 2024 · 2 comments
Open
3 tasks done

Bot will not sync command tree #9717

lucasballr opened this issue Jan 24, 2024 · 2 comments
Labels
unconfirmed bug A bug report that needs triaging

Comments

@lucasballr
Copy link

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:

  1. 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
  1. 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

  • I have searched the open issues for duplicates.
  • I have shown the entire traceback, if possible.
  • I have removed my token from display, if visible.

Additional Context

No response

@lucasballr lucasballr added the unconfirmed bug A bug report that needs triaging label Jan 24, 2024
@No767
Copy link

No767 commented Jan 24, 2024

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()

@No767
Copy link

No767 commented Jan 24, 2024

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 in discord.Client (and all subclasses) instead of trying to attach it as an attribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
unconfirmed bug A bug report that needs triaging
Projects
None yet
Development

No branches or pull requests

2 participants