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?