pythondiscorddiscord.py

Double-up commands when syncing to guild and globally


I am syncing to my testing server guilds as well as globally but this make two commands that do the same thing show up on discord. Image of two commands

Here is my git repo with CappaBot.py being where I am defining and syncing commands.

Here is the ping command

@tree.command(
    description="I will reply with 'pong' as fast as I can."
)
async def ping(interaction: discord.Interaction):
    print("Got ping command")
    print(interaction)
    await interaction.response.send_message("Pong")

And here is where I am syncing

# This will run when the bot is ready to take inputs
@client.event
async def on_ready():
    # Print that we have connected to discord.
    print(f'{client.user} has connected to Discord!')

    # Add the voice commands to the command tree
    voiceGroup = VoiceGroup(name="voice", description="The voice commands can make me connect and disconnect from a voice call.")
    tree.add_command(voiceGroup)

    # Sync to testing servers
    print("Syncing servers...")
    for server in SERVERS:
        server = discord.Object(server)
        # Copy the commands to the server
        tree.copy_global_to(guild=server)
        # Sync the commands to the server
        await tree.sync(guild=server)

    # Sync globally
    print("Syncing globally...")
    await tree.sync()

    if DEBUG:
        user = client.get_user(CAPPABOT)
        await user.send("Cappa Bot has started")
    
    print("Finished loading.")

Note: I know I shouldn't be syncing on ready but I will change that in the future

I tried changing the syncing but that would either make commands not sync globally or not sync immediately for the testing servers.

How do I make it so my testing servers will update immediately but still update globally for other server?

Edit: Look at the github for what I changed. Removed the for loop that syncs to servers and added some commands to clear old commands and sync.

Edit 2: Archived github repo, making a new bot


Solution

  • Your answer is relatively simple.

    First you sync the commands to the specific servers in your for loop, which makes discord create guild specific interactions.

    THEN your bot also syncs them to it's global interactions, which makes discord create global interactions, reachable from everywhere INCLUDING your test server

    if you sync them to all of your servers and globally, you can just remove the for loop, because it indeed creates every command twice, which in this case is the absolutely expected and intended behaviour.