Discordbot.py:NotFound: 404 Not Found (error code: 10062): Unknown interaction

huangapple go评论64阅读模式
英文:

Discordbot.py:NotFound: 404 Not Found (error code: 10062): Unknown interaction

问题

以下是你的代码的翻译部分,代码部分不进行翻译,只返回翻译好的内容:

当我尝试使用斜杠命令时,它告诉我这是一个错误代码:

上述异常是以下异常的直接原因:

跟踪最近的调用(最后一个调用最先):
  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): 未知交互

我已经更改了延迟,还尝试将embed=em放在response.defer()中。我尝试覆盖嵌入式但无济于事,它不起作用。

如果您需要进一步的帮助,请随时告诉我。

英文:

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

答案1

得分: 2

我认为问题在于你的交互在你回应之前超时 - 因此出现 404 错误。你应该将 defer() 放得更高一些 - 最好尽快并且在你进行任何 API 调用之前。

@bot.tree.command(description="使用 chat-gpt 创建一个 txt")
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 的回应:", description=response)
            await asyncio.sleep(delay=0)
            await interaction.followup.send(embed=em)
英文:

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 gpts responce:", description=response)
            await asyncio.sleep(delay=0)
            await interaction.followup.send(embed=em)

huangapple
  • 本文由 发表于 2023年2月9日 03:01:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定