【问题标题】:'Interaction failed.' Discord slash commands“交互失败。”不和谐斜线命令
【发布时间】:2021-11-20 17:17:21
【问题描述】:

它在我最初的几次工作中奏效了。这是随机的。它有时会工作或失败,并显示“交互失败”。主要是失败。请帮忙。

from discord_slash import SlashCommand
client = commands.Bot(command_prefix='!', intents=intents)

slash = SlashCommand(client, sync_commands=True)
@slash.slash(description='Ping.')
async def ping(ctx):
    await ctx.send(f'Current speed - {round(client.latency * 1000)}')

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    为避免出现错误消息"This Interaction failed.",库提供了一个简单的解决方案。

    只需将await ctx.defer() 放在代码的顶部,在您的情况下

    @slash.slash(description='Ping.')
    async def ping(ctx):
        await ctx.defer()
        await ctx.send(f'Current speed - {round(client.latency * 1000)}')
    

    如果机器人需要超过3 秒才能给出答案,API 将返回此类错误。使用ctx.defer() 会绕过这个,机器人会发出一条消息。

    请注意,全局斜杠命令大约需要一个小时才能使任何更改可见/明显。要解决此问题,您可以将guild_ids=[YourGuildID] 添加到您的代码中并对其进行测试。

    【讨论】:

    • 你说的是 discord.py 吗?因为我从 discord.py 迁移到 nextcord。这是一个替代品。而且您的代码似乎不起作用。
    • @Ala 我确实在谈论 discord.py,因为您在问题中对其进行了标记。 defer 方法是您导入的“discord_slash”模块的一部分,因此“nextcord”应该没有问题。究竟是什么不工作?您是否使用过“打印”语句?另外:这段代码并不新鲜,与“discord.py”中的代码相同。
    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 2021-10-19
    • 1970-01-01
    • 2021-06-06
    • 2022-10-20
    • 2021-09-27
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多