英文:
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 gpt’s responce:", description=response)
await asyncio.sleep(delay=0)
await interaction.followup.send(embed=em)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论