I can delete one message or else i get this error message in discord: "The application did not respond"
Or this error message in the console: "discord.app_commands.errors.CommandInvokeError: Command 'clear' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction"
This is my code(the purge command, which causes problems, is in the last shown file):
src/init.py:
import os
import discord
from discord.ext import commands as discord_commands
from dotenv import load_dotenv
from src import commands
def run_discord_bot():
def get_token():
load_dotenv()
return os.environ.get("TOKEN")
token = get_token()
intents = discord.Intents.all()
intents.message_content = True
client = discord_commands.Bot(command_prefix="?", intents=intents)
commands.await_commands(client)
client.run(token)
src/commands/init.py:
import discord
from discord import app_commands
from src.commands import responses
def await_commands(client):
# syncing slash commands
@client.event
async def on_ready():
try:
synced = await client.tree.sync()
print(f"Synced {len(synced)} commands(s)")
except Exception as e:
print(e)
@client.tree.command(name='clear', description="Delete messages")
@app_commands.describe(amount="Number of messages to delete")
async def clear(interaction: discord.Interaction, amount: int):
await responses.clear(interaction, 'clear', amount)
src/commands/responses.py:
async def clear(interaction, command, amount: int):
if amount > 100:
await message(interaction, command, f"The amount of messages to delete is too much", True)
elif amount > 0:
await interaction.channel.purge(limit=amount)
await message(interaction, command, f"{amount} messages have been deleted", True)
elif amount < 0:
await message(interaction, command, f"Can't delete a negative amount of messages", True)
else:
await message(interaction, command, f"0 Messages have been deleted", True)
I already tried a lot of things but nothing seems to work. I just want to use slash commands to delete any amount of messages in a channel.
The "Application does not respond" simply means that your bot does not respond to the command after 3 seconds (while you are purging messages), and you must answer the command within 3 seconds. Therefore, if the amount
is bigger, the purging takes longer time (more than 3 seconds), which will cause this message to appear.
The solution is quite simple by defer
the interaction first and send a followup
after you finished purging all messages.
Some idea:
async def clear(interaction, command, amount: int):
# if amount is between 0 and 100, defer first and purge
if 0 < amount <= 100:
await interaction.response.defer()
# purge the messages
await interaction.followup.send(content="purge finished.")