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

Pycord开发遇Unknown interaction错误:带按钮的Embed支持系统问题

解决Discord机器人Unknown interaction错误

问题原因

你代码里的核心错误是同时使用了ctx.defer()和ctx.response.send_message()。调用defer()已经向Discord发送了初始的"正在处理"响应,此时交互的初始响应通道已被占用,不能再用ctx.response.send_message()发送内容,必须通过followup发送;反之,如果不需要延迟响应,直接发送消息即可,无需调用defer()。

解决方案

提供两种可行的修正方案,根据你的需求选择:

方案1:移除不必要的defer(推荐,无耗时操作时用)

如果你的support命令没有耗时逻辑,直接发送Embed和View即可:

embed = discord.Embed(
        title="Are You Looking For Help?",
        description="Use buttons man.",
        color=discord.Colour.blurple(),
)

class MyView(discord.ui.View):
    @discord.ui.button(label="Open Ticket", style=discord.ButtonStyle.primary, emoji="😎")
    async def button_callback(self, interaction: discord.Interaction, _):
        await interaction.response.send_message("You clicked the button!", ephemeral=True)

@bot.slash_command(name="support")
async def support(ctx):
    view = MyView()
    # 直接发送初始响应,指定ephemeral=True让消息仅自己可见
    await ctx.respond(embed=embed, view=view, ephemeral=True)

bot.run("token")

注:ctx.respond()是discord.py中更简洁的写法,和ctx.response.send_message()效果一致。

方案2:保留defer(适合有耗时操作场景)

如果你的命令需要执行耗时任务(比如查询数据库、调用API),必须先defer告知Discord正在处理,后续用followup发送内容:

embed = discord.Embed(
        title="Are You Looking For Help?",
        description="Use buttons man.",
        color=discord.Colour.blurple(),
)

class MyView(discord.ui.View):
    @discord.ui.button(label="Open Ticket", style=discord.ButtonStyle.primary, emoji="😎")
    async def button_callback(self, interaction: discord.Interaction, _):
        await interaction.response.send_message("You clicked the button!", ephemeral=True)

@bot.slash_command(name="support")
async def support(ctx):
    # 发送延迟响应,标记为仅自己可见
    await ctx.defer(ephemeral=True)
    
    # 这里可以添加耗时操作,比如:
    # await some_long_running_task()
    
    view = MyView()
    # 用followup发送最终内容
    await ctx.followup.send(embed=embed, view=view)

bot.run("token")

额外注意点

  • 按钮回调里的回复:如果不需要后续跟进,直接用interaction.response.send_message()即可,不需要followup(除非已经defer过该按钮交互)。
  • 交互超时:确保你的代码在Discord规定的3秒内发送初始响应(defer或respond),否则也会触发Unknown interaction错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 06:53:24