how do i sync slash commands from cogs in pycord?

171 Views Asked by At

I tried syncing with ctx.bot.tree.sync(), but I always get an error that the Bot object has no attribute tree

Main.py (sync command)

@bot.slash_command(name="sync")
async def sync(ctx): 
    await ctx.defer(ephemeral=True)
    cmds = await ctx.bot.tree.sync()
    await ctx.send(f"succesfully synced {len(cmds)} commands global")
1

There are 1 best solutions below

0
Blue Robin On

Syncing commands in Pycord

The way you sync commands for discord.py and Pycord are different because of how they're structured. In the docs, this is how you sync for Pycord:

 await bot.sync_commands()

Registers all commands that have been added through add_application_command(). This method cleans up all commands over the API and should sync them with the internal cache of commands. It attempts to register the commands in the most efficient way possible, unless force is set to True, in which case it will always register all commands.

See here

This should be done automatically if you didn't override on_connect. There's also a warning under on_connect that says:

Overriding this event will not call Bot.sync_commands(). As a result, ApplicationCommand will not be registered.

To check if the bot connects, use a listener instead.

Basically this allows you to listen to multiple events from different places e.g. such as on_ready()

Example code:

@bot.listen()
async def on_connect():
    print('Bot has connected!')