0

Here is the code

@bot.tree.command(description="create a txt with chat-gpt")
async def gpt(interaction: discord.Interaction, *, prompt:str):
    async with aiohttp.ClientSession() as session:
        payload = {
            "model":"text-davinci-003",
            "prompt":prompt,
            "temperature": 0.5,
            "max_tokens": 50,
            "presence_penalty": 0,
            "frequency_penalty": 0,
            "best_of": 1,
        }
        headers = {"Authorization": f"Bearer {API_KEY}"}
        async with session.post("https://api.openai.com/v1/completions", json=payload, headers=headers) as resp:
            response = await resp.json()
            em = discord.Embed(title="Chat gpt’s responce:", description=response)
            await interaction.response.defer()
            await asyncio.sleep(delay=0)
            await interaction.followup.send(embed=em)

when I try the slash command, it tells me this as an error code

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

Traceback (most recent call last):
  File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/tree.py", line 1242, in _call
    await command._invoke_with_namespace(interaction, namespace)
  File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/commands.py", line 887, in _invoke_with_namespace
    return await self._do_call(interaction, transformed_values)
  File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/commands.py", line 880, in _do_call
    raise CommandInvokeError(self, e) from e
discord.app_commands.errors.CommandInvokeError: Command 'gpt' raised an exception: NotFound: 404 Not Found (error code: 10062): Unknown interaction

And I really can't find the solution even after everything I tried so if you have some time please help me

I changed the delay, I also tried to put the embed=em in response.defer() I tried to override the embed but nothing to do it does not work

TylerH
  • 20,799
  • 66
  • 75
  • 101

1 Answers1

2

I think the issue is that your interaction is timing out before you respond - hence the 404 error. You should put the defer() further up - ideally as soon as possible and before you make any API calls.

@bot.tree.command(description="create a txt with chat-gpt")
async def gpt(interaction: discord.Interaction, *, prompt:str):
    await interaction.response.defer()
    async with aiohttp.ClientSession() as session:
        payload = {
            "model":"text-davinci-003",
            "prompt":prompt,
            "temperature": 0.5,
            "max_tokens": 50,
            "presence_penalty": 0,
            "frequency_penalty": 0,
            "best_of": 1,
        }
        headers = {"Authorization": f"Bearer {API_KEY}"}
        async with session.post("https://api.openai.com/v1/completions", json=payload, headers=headers) as resp:
            response = await resp.json()
            em = discord.Embed(title="Chat gpt’s responce:", description=response)
            await asyncio.sleep(delay=0)
            await interaction.followup.send(embed=em)
ESloman
  • 1,961
  • 1
  • 10
  • 14