Discord bot Python slash command throws 404 Unknown interaction error after executing successfully

I’m working on a Discord bot using Python and I’ve run into a strange issue. I have a slash command that purges messages from a channel, and while the command actually works (it does delete the messages), I keep getting errors when trying to send a response back to the user.

The main problem is that after the command runs, instead of showing how many messages were deleted, Discord displays “The application did not respond” and I get this error in my console:

discord.app_commands.errors.CommandInvokeError: Command 'purge' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction

I’ve tried different approaches like using interaction.response.defer() before the purge operation, and then interaction.followup.send() to send the confirmation message. I also tried interaction.edit_original_response() but nothing seems to work properly.

Here’s my current code:

@client.tree.command(name="purge", description="Removes specified amount of messages from channel")
async def purge_messages(interaction: discord.Interaction, count: int):
    max_limit = 100
    min_limit = 1
    
    if interaction.user.guild_permissions.manage_messages:
        if min_limit < count <= max_limit:
            await interaction.response.defer()
            removed = await interaction.channel.purge(limit=count)
            await interaction.followup.send(f"Successfully removed {len(removed)} messages.")
        else:
            await interaction.response.send_message("Invalid message count provided.", ephemeral=True)
    else:
        await interaction.response.send_message("Insufficient permissions to use this command.", ephemeral=True)

The messages get deleted correctly but the bot never responds with the confirmation message. What am I doing wrong here?

Had this exact problem for weeks with my moderation bot. It’s not your code structure - it’s a timing issue with Discord’s interaction system. Adding a small delay before the followup response fixes it. Wrap your followup in a try-except block and add await asyncio.sleep(0.5) before the followup.send call. Also make sure you’re not accidentally calling multiple response methods elsewhere in your code - that’ll invalidate the interaction token. Check if the interaction’s still valid before sending the followup with interaction.is_expired() returning False. These tokens don’t last long and bulk operations like purging can mess with the timing.

hey, sounds like a timing issue! try adding ephemeral=True to your followup.send() call. discord can get confused with public responses after a defer. also, keep an eye on if you’re hitting rate limits when purging a lot of messages.

This happens because Discord’s interaction tokens expire after 15 minutes, but they can also become invalid if there’s a network hiccup during the purge. I’ve hit this exact issue with my utility bot - it usually happens when the purge takes longer than expected, especially with older messages. Here’s what fixed it for me: add if not interaction.is_expired(): before your followup.send() call. Better yet, use interaction.response.send_message() to acknowledge first, then edit that message after the purge finishes with interaction.edit_original_response(). This keeps a stable connection with Discord’s API and prevents timeouts during bulk operations.