我已经使用交互在我的不和谐机器人中添加了临时命令。 我推迟了第一行代码的交互,以免终止命令 这是我的代码:
@commands.hybrid_command()
async def hello(self, ctx: commands.Context[Any], *, link: str) -> None:
await ctx.interaction.response.defer()
await create_channel(ctx, "test channel")
channel_ctx: "MessageableChannel" = ctx.channel
response = "hello discord member"
await send_ephemeral_message(ctx=ctx, response=response)
这是我发送临时消息的功能:
async def send_ephemeral_message(ctx, response):
"""This function will check if the message is an interaction and if it is, it will send an ephemeral message.
Used to send normal message in testing environment.
"""
ctx = ctx if ctx and isinstance(ctx, commands.Context) else None
interaction = ctx.interaction if ctx else None
print('context', ctx and interaction)
if ctx and interaction:
await interaction.followup.send(response, ephemeral=True)
print('ephemeral message sent')
# send non-ephemeral message in testing environment
else:
await ctx.send(response)
有时我会收到此错误:
NotFound:404 Not Found(错误代码:10062):未知交互
有时我会收到此错误:
HTTPException:400 错误请求(错误代码:40060):交互已被确认。
我该如何解决这个问题?
“404 Not Found”错误表示未找到正在响应的交互,而“400 Bad Request”错误表示交互已被确认。这些错误可能是由于竞争条件或交互处理不正确而导致的。
要解决“404 Not Found”错误,请确保在发送临时消息之前正确推迟交互。另外,请确保回复时交互仍然有效。
要处理“400 错误请求”错误,请确保您没有尝试多次响应交互或在交互已被确认后响应。在发送临时消息之前检查交互是否已被确认。
您可以采取一些步骤来解决这些问题:
确保在执行任何其他操作之前推迟命令功能开始时的交互。
在回复之前检查交互是否仍然有效。您可以通过验证交互是否不是 None 并确保尚未确认来完成此操作。
避免多次发送临时消息或在交互确认后发送临时消息。您可以跟踪交互状态以防止这种情况发生。
仔细检查您的代码逻辑,以确保正确处理交互,尤其是在可能发生竞争条件的异步环境中。
注意:通过解决这些问题,您应该能够修复 Discord 机器人中的“404 Not Found”和“400 Bad Request”错误。