You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py按钮交互超时显示“This Interaction Failed”修复求助

问题解决:Discord按钮交互超时失败(设置timeout=None报错)

问题原因

当设置timeout=None让视图持久化时,Discord要求所有交互组件(如按钮)必须指定唯一的custom_id,否则无法识别持久化的交互请求;同时Bot需要提前注册这个持久视图,才能持续处理后续的按钮点击事件。

修复步骤

1. 给按钮添加唯一custom_id

修改Menu类,为按钮指定自定义ID,确保每个按钮的ID唯一:

class Menu(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)
        self.value = None

    # 新增custom_id参数,保证ID唯一
    @discord.ui.button(label="Script", style=discord.ButtonStyle.green, emoji="📜", custom_id="persistent_script_button")
    async def script(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message("Hello world", ephemeral=True)

2. 在Bot启动时注册持久视图

在Bot初始化完成后,添加视图注册代码,让Bot持续监听该视图的交互:

# 假设你的client是discord.Client或commands.Bot实例
client.add_view(Menu())

3. 保持斜线命令代码不变

你的斜线命令逻辑无需修改,正常初始化视图并发送消息即可:

@client.tree.command(name="test", description="this is test for button")
async def test(interaction: discord.Interaction):
    if not interaction.user.guild_permissions.administrator:
        return await interaction.response.send_message("You are not admin")
    else:
        view = Menu()
        embed = discord.Embed(title="test", color=0xfed9ff)
        await interaction.response.send_message(embed=embed, view=view)

额外提示

  • 如果Bot需要重启后仍能处理之前发送的按钮,必须保证每次启动都注册该视图,且custom_id保持不变。
  • 不要重复使用相同的custom_id,否则会引发交互处理冲突。

内容的提问来源于stack exchange,提问作者cszn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 17:46:01