Issue
My slash commands return 404 Not Found (error code: 10062): Unknown interaction when I run them. I do have them deferred as:
@bot.tree.command(name="evaluate")
async def evaluate(interaction: discord.Interaction):
await interaction.response.defer(ephemeral=True)
await asyncio.sleep(10)
await interaction.followup.send("Command works")
for example.
However, when I run my commands, it raises the following error:
File "/home/container/.local/lib/python3.11/site-packages/discord/app_commands/commands.py", line 862, in _do_call
return await self._callback(interaction, **params) # type: ignore
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/container/bot.py", line 282, in evaluate
await interaction.response.defer(ephemeral=False, thinking=True)
File "/home/container/.local/lib/python3.11/site-packages/discord/interactions.py", line 636, in defer
await adapter.create_interaction_response(
File "/home/container/.local/lib/python3.11/site-packages/discord/webhook/async_.py", line 218, in request
raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction
This happens mostly in one command, but at the very end. The entire command works fine but the bot doesn't send the final confirmation response await interaction.followup.send() and instead throws the above error. (Edited paragraph)
Reason for posting:
I don't really think I'm making an error with the code itself, which is why I'd like some insight as to why this is or may be happening. I also posted this in case someone else in the future encounters the same issue, since I couldn't find similar or same questions asked here on SO.
Posted this on the discord.py official Discord Server and got an answer.
If a 404 Not Found error appears even after deferring, it means that it took too long for the defer() to execute. Or in other words, your await interaction.response.defer() is running after 3 seconds have passed and the API request has already been terminated.
Credit: SolsticeShard on the discord.py Discord.
The solution was to run defer() much earlier up the code block. That ensures that the interaction is deferred before the 3s time limit has passed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With