I created a simple Discord bot. The ‘clear’ command works, but instead of saying ‘{len(deleted)} messages have been deleted.’, it shows a warning saying ‘The application did not respond.’Additionally, I’m receiving this error:
'discord.app_commands.errors.CommandInvokeError: Command 'clear' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction'."
I tried using the “interaction.response.defer()” command to delay the process. The “Application did not respond” warning on Discord disappeared, but now it doesn’t show how many messages were deleted; instead, it doesn’t display anything. I also tried sending a message with the “interaction.followup.send” command and editing the original response with “interaction.edit_original_response,” but neither of them worked.
My code:
@bot.event
async def on_ready():
await bot.load_extension("admin")
try:
synced= await bot.tree.sync()
print(f"The number of integrated '/' commands is {len(synced)}." )
except ImportError as e:
print(e)
print(f'Bot logged in as {bot.user.name}.')
@bot.tree.command(name="clear", description="It clears the specified number of messages from the chat.")
async def clear(interaction: discord.Interaction, number: int):
max_messages = 100
min_messages = 1
if interaction.user.guild_permissions.administrator:
if min_messages < number <= max_messages:
interaction.response.defer()
deleted = await interaction.channel.purge(limit=number)
await interaction.followup.send(f"{len(deleted)} messages have been deleted.")
else:
await interaction.response.send_message("The number of messages you entered is invalid.", ephemeral=True)
else:
await interaction.response.send_message("You do not have the necessary permissions.", ephemeral=True)
TOKEN = "BOT_TOKEN"
bot.run(TOKEN)
I just wanted my bot to indicate how many messages were deleted as written in the code.
Aktuğ İ. is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.