discord.py斜杠命令在长时间运行的函数上“此交互失败”

2024-09-29 17:15:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个长时间运行的功能,大约需要20秒左右才能完成。函数完成后,它将结果作为消息发送给Discord

如果我通过输入一个常用命令(没有斜杠命令)来运行这个函数,它会完全正常工作,并且我会看到结果发送到Discord

但是,如果我通过斜杠命令调用函数,Discord会告诉我“此交互失败”,然后在消息中看到结果后不久。因此,该函数工作得非常好,但斜杠命令似乎认为它们失败了

为了测试这一点,我得到了一个基本函数:

@slash.slash(name="test",
             guild_ids=[123,456],
             description="This is just a test command, nothing more.",
             options=[
                 create_option(
                     name="optone",
                     description="This is the first option we have.",
                     option_type=3,
                     required=False
                 )
             ])
async def test(ctx, optone: str):
    await ctx.send(content=f"I got you, you said {optone}!")

如果我通过斜杠命令调用它,没有问题。然后我在await ctx.send...上放置一个断点,然后再次运行该命令。Discord说“这种互动失败了”。我继续调试执行,然后发送消息

为什么斜杠命令认为命令失败了,而它没有失败,我该如何修复它


Tags: 函数nametest命令消息isdescriptionawait
1条回答
网友
1楼 · 发布于 2024-09-29 17:15:36

您应该包括您的软件包使用的斜杠命令,但是(从命令语法来看)我假设您使用的是discord-py-slash-command

packagediscord's developer文档中:

In this example we responded directly to the interaction, however if you want to delay the response (if you need more than 3 seconds before sending a message) you can defer the response for up to 15 minutes with ctx.defer(), this displays a “Bot is thinking” message. However do not defer the response if you will be able to respond (send) within three seconds as this will cause a message to flash up

将ctx.defer()添加到命令的顶部。 如果您需要的时间超过15秒,您可以发送一条消息,稍后再进行编辑

相关问题 更多 >

    热门问题