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

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

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

问题

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

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

  1. 上述异常是以下异常的直接原因:
  2. 跟踪最近的调用(最后一个调用最先):
  3. File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/tree.py", line 1242, in _call
  4. await command._invoke_with_namespace(interaction, namespace)
  5. File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/commands.py", line 887, in _invoke_with_namespace
  6. return await self._do_call(interaction, transformed_values)
  7. File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/commands.py", line 880, in _do_call
  8. raise CommandInvokeError(self, e) from e
  9. 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

  1. @bot.tree.command(description="create a txt with chat-gpt")
  2. async def gpt(interaction: discord.Interaction, *, prompt:str):
  3. async with aiohttp.ClientSession() as session:
  4. payload = {
  5. "model":"text-davinci-003",
  6. "prompt":prompt,
  7. "temperature": 0.5,
  8. "max_tokens": 50,
  9. "presence_penalty": 0,
  10. "frequency_penalty": 0,
  11. "best_of": 1,
  12. }
  13. headers = {"Authorization": f"Bearer {API_KEY}"}
  14. async with session.post("https://api.openai.com/v1/completions", json=payload, headers=headers) as resp:
  15. response = await resp.json()
  16. em = discord.Embed(title="Chat gpts responce:", description=response)
  17. await interaction.response.defer()
  18. await asyncio.sleep(delay=0)
  19. await interaction.followup.send(embed=em)

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

  1. The above exception was the direct cause of the following exception:
  2. Traceback (most recent call last):
  3. File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/tree.py", line 1242, in _call
  4. await command._invoke_with_namespace(interaction, namespace)
  5. File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/commands.py", line 887, in _invoke_with_namespace
  6. return await self._do_call(interaction, transformed_values)
  7. File "/home/haoroux/.local/lib/python3.10/site-packages/discord/app_commands/commands.py", line 880, in _do_call
  8. raise CommandInvokeError(self, e) from e
  9. 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 调用之前。

  1. @bot.tree.command(description="使用 chat-gpt 创建一个 txt")
  2. async def gpt(interaction: discord.Interaction, *, prompt:str):
  3. await interaction.response.defer()
  4. async with aiohttp.ClientSession() as session:
  5. payload = {
  6. "model": "text-davinci-003",
  7. "prompt": prompt,
  8. "temperature": 0.5,
  9. "max_tokens": 50,
  10. "presence_penalty": 0,
  11. "frequency_penalty": 0,
  12. "best_of": 1,
  13. }
  14. headers = {"Authorization": f"Bearer {API_KEY}"}
  15. async with session.post("https://api.openai.com/v1/completions", json=payload, headers=headers) as resp:
  16. response = await resp.json()
  17. em = discord.Embed(title="Chat gpt 的回应:", description=response)
  18. await asyncio.sleep(delay=0)
  19. 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.

  1. @bot.tree.command(description="create a txt with chat-gpt")
  2. async def gpt(interaction: discord.Interaction, *, prompt:str):
  3. await interaction.response.defer()
  4. async with aiohttp.ClientSession() as session:
  5. payload = {
  6. "model":"text-davinci-003",
  7. "prompt":prompt,
  8. "temperature": 0.5,
  9. "max_tokens": 50,
  10. "presence_penalty": 0,
  11. "frequency_penalty": 0,
  12. "best_of": 1,
  13. }
  14. headers = {"Authorization": f"Bearer {API_KEY}"}
  15. async with session.post("https://api.openai.com/v1/completions", json=payload, headers=headers) as resp:
  16. response = await resp.json()
  17. em = discord.Embed(title="Chat gpts responce:", description=response)
  18. await asyncio.sleep(delay=0)
  19. 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:

确定