pythondiscorddiscord.py

Discord command not showing when bot is up


I'm working on a discord bot using discord.py==2.7.0a5350+g8f2cb607.
This bot is deployed in a single server and will not be deployed anywhere else.

I've my on_ready() function like this :

@bot.event
async def on_ready():
    print(f"Connected as {bot.user}")
    guild_id = discord.Object(id=GUILD_ID)
    synced = await bot.tree.sync(guild=guild_id)
    print(f"{len(synced)} commands synced on server {GUILD_ID}.")

When I'm running it, I receive this logs :

PS D:\MyBot> py main.py
2025-11-11 19:02:49 INFO     discord.client logging in using static token
2025-11-11 19:02:50 INFO     discord.gateway Shard ID None has connected to Gateway (Session ID: some_session_id).
Connected as My-Bot#1111
0 commands synced on server My(correct)-Guild-ID

As this bot is for one server only, I'm using the .sync command with the ID of the server, so it's not general and should not take an hour to sync new commands.
The problem is that this is not working, and my new commands are never showing up.
I have to change my on_ready() each time I create a new command.

Here's an example of my commands :

@bot.tree.command(name='leave', description='Leave vc')
async def leave_call(interaction: discord.Interaction):
    vc = discord.utils.get(bot.voice_clients, guild=interaction.guild)
    if vc and vc.is_connected():
        if isinstance(vc, voice_recv.VoiceRecvClient):
            vc.stop_listening()
        await vc.disconnect()
        await interaction.response.send_message("Disconnected.", ephemeral=True)
    else:
        await interaction.response.send_message("Not connected.")

I don't really understand what's the problem here, I've tried to search here, on Discord.py doc, even with several IA (ChatGPT, Copilot, Perplexity, and Mistral), but i can't find anything.
I created an other bot years ago but don't remember how I did to make commands available instantly.


Solution

  • To make it work normally I just had to set my guild_id on my commands, so they are forced for this server only :

    @bot.tree.command(guild=guild_id, name='leave', description='Leave vc')
    async def leave_call(interaction: discord.Interaction):
        vc = discord.utils.get(bot.voice_clients, guild=interaction.guild)
        if vc and vc.is_connected():
            if isinstance(vc, voice_recv.VoiceRecvClient):
                vc.stop_listening() 
                await vc.disconnect() 
                await interaction.response.send_message("Disconnected.", ephemeral=True)
            else:
                await interaction.response.send_message("Not connected.")
    

    It's now working instantly and the logs returns the good value :

    PS D:\MyBot> py main.py
    2025-11-11 19:02:49 INFO discord.client logging in using static token
    2025-11-11 19:02:50 INFO discord.gateway Shard ID None has connected to Gateway (Session ID: some_session_id).
    Connected as My-Bot#1111
    7 commands synced on server My(correct)-Guild-ID