Python Discord Bot Command Has Slow Response Time on Subsequent Execution

I’m building a Discord bot in Python and running into a performance issue. I created a command that only works when someone is connected to a voice channel. The command posts a message and modifies the voice channel settings.

The first time someone uses the command, it works fine. But when another user tries to run it after the first person leaves the voice channel, the bot either doesn’t respond or takes around 5 minutes to process the request.

Here’s my command handler:

@client.command(name="findteam", pass_context=True)
async def findteam_command(ctx):
    await teamfinder.start_search(ctx, client)

Main function:

async def start_search(ctx, client):
    target_channel_id = 123456789012345678
    if ctx.channel == client.get_channel(target_channel_id):
        user = ctx.author
        if user.voice is not None:
            temp_category = client.get_channel(987654321098765432).category
            if user.voice.channel.category == temp_category:
                await helpers.activate_search(user, client)
            else:
                error_channel = client.get_channel(111222333444555666)
                await error_channel.send(
                    user.mention + ", please join a temporary voice channel first.",
                    delete_after=25)
        else:
            error_channel = client.get_channel(111222333444555666)
            await error_channel.send(
                user.mention + ", you must be in a voice channel to use this feature.", 
                delete_after=25)

Channel modification function:

async def activate_search(user, client):
    voice_channel = user.voice.channel
    announcement_channel = client.get_channel(123456789012345678)
    player_rank = await fetch_user_rank(user)
    
    restricted_role = get(user.guild.roles, id=777888999000111222)
    await voice_channel.set_permissions(restricted_role, connect=False)
    await voice_channel.edit(name="Team Search Active", user_limit=4)
    
    notification = await announcement_channel.send(
        content=user.mention + " wants to form a team! Rank: " + player_rank.name + ". React to join. Current players: " + str(
            len(user.voice.channel.members)) + "/4",
        delete_after=600)
    await notification.add_reaction('👍')
    
    search_data[user.id] = ["temp", notification, voice_channel]
    search_data[notification.id] = [user, "temp", voice_channel]
    search_data[voice_channel.id] = [user, notification, "temp"]

When the original user leaves, everything resets:

async def reset_channel(channel):
    message = find_message(channel)
    original_user = find_original_user(channel)
    
    await message.delete()
    if len(channel.members) > 0:
        restricted_role = get(original_user.guild.roles, id=777888999000111222)
        await channel.set_permissions(restricted_role, connect=True)
        new_owner = channel.members[0]
        await channel.edit(name=new_owner.display_name + "'s Room", user_limit=None)
    
    for key in [message.id, channel.id, original_user.id]:
        search_data.pop(key, None)

Could there be an infinite loop or blocking operation causing this delay? Any ideas what might be causing the slow response time?

Had the same problem with my Discord bot. The issue was channel permission changes. When you use set_permissions() on voice channels, Discord needs time to sync those changes across their servers. If someone runs the command right after a permission reset, the bot sits there waiting for the update to finish. Your search_data dictionary cleanup might be creating race conditions too - if a new user triggers the command before cleanup finishes, you’ll get stale data conflicts. Add error handling around permission operations and throw in a short cooldown after channel resets. Also check if fetch_user_rank() makes external API calls that could timeout.

you’re not handling voice state updates right. Discord doesn’t instantly update your bot’s cache when users disconnect from channels. add a voice state update event listener to catch joins/leaves. that search_data dict is probably holding old references too - clean that up. the delay happens because your bot waits for Discord to sync voice states automatically.

This is a caching issue with Discord’s API. Your bot’s holding onto old voice channel states after users disconnect, so when you run commands later, it’s working with outdated member data. I’ve hit this exact problem with my music bot - user.voice.channel.members would show wrong info after people left. The 5-minute delay means Discord’s cache eventually refreshes, but your bot isn’t forcing it when it should. Try await client.wait_until_ready() before channel operations, or add a quick asyncio.sleep(1) before checking voice states. You can also force fresh data by using client.fetch_channel() instead of client.get_channel() for important stuff. Another thing - check if your search_data dictionary has references to disconnected users. That’ll make your bot wait for operations on voice states that don’t exist anymore.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.