Pycord: Unknown Interaction

414 Views Asked by At
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

2

There are 2 best solutions below

0
Hazzu On BEST ANSWER

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()
0
Karo On

I already tried stuff like defer and such, I have no idea what exactly the problem is, it worked with "ctx.reply" just some hours ago.

Solved. The problem was that the defer was low placed in the code hirarchy