pythondiscord.pybots

Slash delete command error unknown interaction


I need some help here I have this delete command with 2 options (amount and all).

When I call amount, it deletes the message but discord bot stays thinking and then says app not responding, it only works fine if i delete just 1 message. and if i use to clean all.

But if it's more than 1, it does the job but throws the error in discord and in console I see:

An error occurred: 404 Not Found (error code: 10062): Unknown interaction

This is my slash command for deletion

@bot.tree.command(name="cleanup", description="Well... it cleans... what else do you think it does (:)")
@commands.has_permissions(manage_messages=True)
@app_commands.describe(all="Delete all messages in this channel",
                       amount="Delete a specific amount of messages (1 to 100)")
async def cleanup(interaction: discord.Interaction, all: bool = False, amount: int = None):
    try:
        if all:
            await interaction.channel.purge(limit=None)
            await interaction.response.send_message(f"All messages in this channel have been deleted.")
        elif amount:
            if amount < 1 or amount > 100:
                await interaction.response.send_message(f"Please enter a number between 1 and 100 for the 'amount' option.")
            else:
                await interaction.channel.purge(limit=amount)
                await interaction.response.send_message(f"{amount} messages have been deleted in this channel.", ephemeral=True)
        else:
            await interaction.response.send_message(f"Please provide one of the options: 'all' or 'amount'.")
    except Exception as e:
        print(f"An error occurred: {e}")

I tried chat gpt but it keeps throwing old `discord_slash_commands` deprecated version commands haha

Solution

  • You have to respond within 3 seconds, no matter what. You're purging before responding, so you probably exceed the deadline.

    If you have to do something slow in an app command, you can use defer(), and followup to respond later on.

    P.s. all is a built-in keyword in Python, as the syntax highlighting (and your Linter) suggest. Don't shadow built-ins.