Search code examples
pythondiscordpycord

Pycord Unknown interaction


I'm trying to make a embed message with buttons. For support system but I'm getting Unknown interaction error. I added ephemeral=True to defer.

Also changed this:

interaction.response.send_messageto this interaction.followup.send()

embed = discord.Embed(
        title="Are You Looking For Help?",
        description="Use buttons man.",
        color=discord.Colour.blurple(),
)

class MyView(discord.ui.View):
    @discord.ui.button(label="Open Ticket", style=discord.ButtonStyle.primary, emoji="😎")
    async def button_callback(self, interaction: discord.Interaction, _):
        await interaction.followup.send("You clicked the button!")

@bot.slash_command(name="support")
async def support(ctx):
    await ctx.defer(ephemeral=True)

    view = MyView()
    await ctx.response.send_message(embed=embed, view=view)
bot.run("token")

Error:

C:\Users\playe\PycharmProjects\pythonProject\.venv\Scripts\python.exe "C:\Users\playe\PycharmProjects\Minecraft Player Bot\main.py" 
Lolo RS's Main#6223
Ignoring exception in command support:
Traceback (most recent call last):
  File "C:\Users\playe\PycharmProjects\pythonProject\.venv\Lib\site-packages\discord\commands\core.py", line 131, in wrapped
    ret = await coro(arg)
          ^^^^^^^^^^^^^^^
  File "C:\Users\playe\PycharmProjects\pythonProject\.venv\Lib\site-packages\discord\commands\core.py", line 1013, in _invoke
    await self.callback(ctx, **kwargs)
  File "C:\Users\playe\PycharmProjects\Minecraft Player Bot\main.py", line 35, in support
    await ctx.defer(ephemeral=True)
  File "C:\Users\playe\PycharmProjects\pythonProject\.venv\Lib\site-packages\discord\interactions.py", line 748, in defer
    await self._locked_response(
  File "C:\Users\playe\PycharmProjects\pythonProject\.venv\Lib\site-packages\discord\interactions.py", line 1243, in _locked_response
    await coro
  File "C:\Users\playe\PycharmProjects\pythonProject\.venv\Lib\site-packages\discord\webhook\async_.py", line 220, in request
    raise NotFound(response, data)
discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\playe\PycharmProjects\pythonProject\.venv\Lib\site-packages\discord\bot.py", line 1130, in invoke_application_command
    await ctx.command.invoke(ctx)
  File "C:\Users\playe\PycharmProjects\pythonProject\.venv\Lib\site-packages\discord\commands\core.py", line 376, in invoke
    await injected(ctx)
  File "C:\Users\playe\PycharmProjects\pythonProject\.venv\Lib\site-packages\discord\commands\core.py", line 139, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.errors.ApplicationCommandInvokeError: Application Command raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interactio

n


Solution

  • Edit: Based on furhter information, I would say that the issue also intermittently comes from a slow internet connection with discord from the computer your bot is running on. This means that upon recieving the interaction on your computer, the interaction will already have timed out, simply because it didn't arrive fast enough to you. The fact that the interaction is not found means that, when your code responded to it, discord already deleted it from their system, because you need to respond to it in ~3s or so. Doing ctx.defer() is also considered responding to the interaction.

    The issue you are facing seems to come from multiple small mistakes in how to handle interactions in the pycord library. If you want to jump right in, you have my working code below, but here are some explanations on your mistakes, and how I fixed them:

    1. Mixing up discord.Interaction and discord.ApplicationContext: Buttons, and other UI elements and features, return the first one, but slash command the second. In truth, ApplicationContext is a wrapper around Interaction. In your slash command, you are calling ctx.response.send_message, but doing ctx.respond is actually the correct thing, as everything is handled by the library.

    2. Bad parameter order in your button callback: The callback should by specification have the following parameters, in order: self, button: discord.Button (the button itself), interaction: discord.Interaction (the interaction). However, you put the interaction first.

    3. Calling interaction.followup.send() instead of interaction.respond() or response.send_message(): This interaction is different from the one you had with the button component, and has never been responded to before. The .respond method is a shortcut to do the correct one without worrying about having to think about it. Also, you used interaction.followup.send()(message) with the parentheses twice, which should not be the case.

    embed = discord.Embed(
        title="Are You Looking For Help?",
        description="Use buttons man.",
        color=discord.Colour.blurple(),
    )
    class MyView(discord.ui.View):
        @discord.ui.button(label="Open Ticket", style=discord.ButtonStyle.primary, emoji="😎")
        async def button_callback(self, button: discord.Button, interaction: discord.Interaction):
            await interaction.response.defer(ephemeral=True)
            await interaction.respond("You clicked the button!", ephemeral=True)
    
    @bot.slash_command(name="support")
    async def support(ctx: discord.ApplicationContext):
        await ctx.defer(ephemeral=True)
        view = MyView()
        await ctx.respond(embed=embed, view=view)
    

    Let me know if anything is unclear in a comment and I will try to edit my response.