pythondiscorddiscord.pypycorddiscord-interactions

Pycord: Unknown Interaction


import discord
from discord.ext import commands, tasks
import imageio
import os
import numpy as np
import asyncio

 @stocks.command(name="graph", description="Generate and display a stock price history graph")
    async def graph(self, interaction: discord.Interaction, symbol):
      video_path = "stock_graph.mp4"
      imageio.mimsave(video_path, ani_frames, fps=8)
      message = await interaction.response.defer()
      asyncio.sleep(10)
      await interaction.followup.send(file=discord.File(video_path))
      plt.close()
            

Error: discord.errors.NotFound: 404 Not Found (error code: 10062): Unknown interaction


Solution

  • First of all, understand that a Discord interaction has a short lifetime, which is exactly 3 seconds. That is, you must respond to the interaction that called your command within 3 seconds or else it will become unknown.

    If your command performs a time-consuming task, where you can't respond to the interaction in less than 3 seconds, you can defer the interaction response (which will cause your bot to go into status thinking) and then send a followup message using await interaction.followup.send().

    async def graph(self, interaction: discord.Interaction, symbol):
        await interaction.response.defer()    # put this on the start
        video_path = "stock_graph.mp4"
        imageio.mimsave(video_path, ani_frames, fps=8)   # maybe this task is taking long?
        asyncio.sleep(10)   # why are you sleeping 10 seconds?
        await interaction.followup.send(file=discord.File(video_path))
        plt.close()