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

discord.py开发Discord机器人出现“This Interaction failed”按钮交互问题

问题修复方案

问题根源

你遇到的"This Interaction failed"是因为在on_member_join事件中调用了await view.wait(),该调用会阻塞当前协程,导致Discord无法正确将按钮交互绑定到对应的View实例;同时默认的View超时设置也可能导致按钮提前失效。

修复步骤及代码调整

  1. 移除await view.wait():不需要在发送消息后等待View结束,Discord.py会自动处理按钮交互事件。
  2. 设置View永久有效(可选):通过初始化方法将View的timeout设为None,避免按钮因超时失效。
  3. 可选:点击后停止View:在按钮回调中调用self.stop(),让按钮点击一次后就失效。
  4. 优化消息发送(可选):给拒绝按钮的回复加上ephemeral=True,避免管理员频道被刷屏。

修改后的完整代码:

class SimpleView(discord.ui.View):
    def __init__(self):
        super().__init__(timeout=None)  # 让按钮永久有效,直到手动停止或bot重启

    @discord.ui.button(label="✔️", style=discord.ButtonStyle.success)
    async def approve(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message(content="Approved!", ephemeral=True)
        self.stop()  # 点击后停止View,按钮失效

    @discord.ui.button(label="❌", style=discord.ButtonStyle.red)
    async def deny(self, interaction: discord.Interaction, button: discord.ui.Button):
        await interaction.response.send_message(content="Denied!", ephemeral=True)
        self.stop()  # 点击后停止View,按钮失效

@bot.event
async def on_member_join(member: discord.Member):
    view = SimpleView()

    role = discord.utils.get(member.guild.roles, name="Test")
    approve_channel_id = 1158716789103013898
    approve_channel = member.guild.get_channel(approve_channel_id)

    if approve_channel is not None:
        await member.add_roles(role)
        await approve_channel.send(f"{member} joined the server! Do you Approve?", view=view)
    else:
        print(f"Channel with ID {approve_channel_id} not found!")

额外说明

  • 如果希望按钮一直保持可用(比如同一消息允许多次操作),可以去掉回调里的self.stop()。
  • 确保bot拥有足够的权限:管理角色、发送消息、使用应用命令等权限,缺少权限也可能导致交互失败(不过你的情况控制台无报错,大概率不是权限问题)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 07:06:26