I’m using discord.py, and one of my commands is “purge”. The problem being is that everytime I use the purge command, it deletes the amount of messages, but the “follow-up” response it is supposed to send causes an error. BE AWARE: This is a SLASH COMMAND.
(Please bear with me, I’m not entirely scripting this myself. I am still learning Python, so I use AI to help me script without sharing my token to it.)
Tried changing the code more than once, expected it to work normally and send “Purged {amount} messages”, but it shows up with “Unknown message” in the error.
Purge command:
`@tree.command(
name=”purge”,
description=”Deletes a specified number of messages from the channel.”
)
async def purge(interaction: Interaction, amount: int):
await interaction.response.defer() # Defers the interaction, giving you more time
try:
deleted = await interaction.channel.purge(limit=amount + 1) # +1 to account for the command message itself
await interaction.followup.send(f”Purged {len(deleted) – 1} messages.”) # Use followup to send the final response
except Exception as e:
await interaction.followup.send(f”An error occurred: {e}”)
@purge.error
async def purge_error(interaction: Interaction, error):
try:
if interaction.response.is_done():
await interaction.followup.send(“An error occurred while executing this command.”)
else:
if isinstance(error, commands.MissingPermissions):
await interaction.response.send_message(“You don’t have permission to use this command.”)
elif isinstance(error, commands.MissingRequiredArgument):
await interaction.response.send_message(“Please specify the number of messages to purge.”)
elif isinstance(error, commands.BadArgument):
await interaction.response.send_message(“Please provide a valid number.”)
else:
await interaction.response.send_message(“An error occurred while executing this command.”)
except Exception as e:
print(f”Failed to send error message: {e}”)`
Error Traceback:
`Task exception was never retrieved
future: <Task finished name=’CommandTree-invoker’ coro=<CommandTree._from_interaction..wrapper() done, defined at /usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/app_commands/tree.py:1107> exception=NotFound(‘404 Not Found (error code: 10008): Unknown Message’)>
Traceback (most recent call last):
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/app_commands/commands.py”, line 858, in do_call
return await self.callback(interaction, **params) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/psylar/Documents/PsylarBot/bot_commands.py”, line 26, in purge
await interaction.followup.send(f”Purged {len(deleted) – 1} messages.”) # Use followup to send the final response
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/webhook/async.py”, line 1843, in send
data = await adapter.execute_webhook(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/webhook/async.py”, line 221, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/app_commands/tree.py”, line 1268, in _call
await command._invoke_with_namespace(interaction, namespace)
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/app_commands/commands.py”, line 883, in _invoke_with_namespace
return await self._do_call(interaction, transformed_values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/app_commands/commands.py”, line 876, in _do_call
raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command ‘purge’ raised an exception: NotFound: 404 Not Found (error code: 10008): Unknown Message
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/app_commands/tree.py”, line 1109, in wrapper
await self._call(interaction)
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/app_commands/tree.py”, line 1271, in _call
await command.invoke_error_handlers(interaction, e)
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/app_commands/commands.py”, line 802, in invoke_error_handlers
await self.on_error(interaction, error) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/home/psylar/Documents/PsylarBot/bot_commands.py”, line 31, in purge_error
await interaction.followup.send(“An error occurred while executing this command.”)
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/webhook/async.py”, line 1843, in send
data = await adapter.execute_webhook(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “/usr/local/lib/python3.12/dist-packages/discord.py-2.4.0a5033+gf77ba711-py3.12.egg/discord/webhook/async.py”, line 221, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message`