# Sync examples and details What I've been recommending to people, is to maintain a sync command as a Message Command (`@bot.command()`) or some sort of message invoke. Bots without the message content intent will still receive content of messages that mention them or are in DMs. ~~It's not like you can use a slash command to sync... when that command isn't synced.~~ ## Syncing gotchas There is a ratelimit on syncing global commands which add commands. Updating commands (currently) has no ratelimit. It is still recommended to sync your commands to a test guild before syncing them globally. Discord.py has added a helper method to do just that: [`CommandTree.copy_global_to`](https://discordpy.readthedocs.io/en/latest/interactions/api.html#discord.app_commands.CommandTree.copy_global_to) This util is used as follows: ```python # you have a defined Bot, with a tree and have commands added to it that are global. guild = ctx.guild or discord.Object(id=...) # you can use a full discord.Guild as the method accepts a Snowflake Bot.tree.copy_global_to(guild=guild) ``` All this method does is copy your defined global commands (so ones without a `guild` or `guilds` kwarg, or without the `@app_commands.guilds()` decorator) to the specified guild within the CommandTree. When you use this method you **must** sync afterward still, you can refer to `when_to_sync.md` for details there. ## Sync command example

```python from typing import Literal, Optional from discord.ext import commands from discord.ext.commands import Greedy, Context # or a subclass of yours @bot.command() @commands.guild_only() @commands.is_owner() async def sync( ctx: Context, guilds: Greedy[discord.Object], spec: Optional[Literal["~", "*", "^"]] = None) -> None: if not guilds: if spec == "~": synced = await ctx.bot.tree.sync(guild=ctx.guild) elif spec == "*": ctx.bot.tree.copy_global_to(guild=ctx.guild) synced = await ctx.bot.tree.sync(guild=ctx.guild) elif spec == "^": ctx.bot.tree.clear_commands(guild=ctx.guild) await ctx.bot.tree.sync(guild=ctx.guild) synced = [] else: synced = await ctx.bot.tree.sync() await ctx.send( f"Synced {len(synced)} commands {'globally' if spec is None else 'to the current guild.'}" ) return ret = 0 for guild in guilds: try: await ctx.bot.tree.sync(guild=guild) except discord.HTTPException: pass else: ret += 1 await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.") ```