Discord.py按钮交互报错404(10015):如何编辑原始响应嵌入?
Discord.py斜杠命令按钮编辑消息报404(Unknown Webhook)的原因与解决方法
错误原因
错误码10015(Unknown Webhook)的核心问题是你要编辑的消息不是交互的原始响应消息:
- 如果你用
interaction.response.defer()后调用followup.send()发送内容,这条followup消息是独立的后续消息,而非初始交互响应。而edit_original_response()只能编辑由interaction.response.send_message()直接发送、或defer()后用edit_original_response()发送的原始响应消息,对followup消息调用该方法自然找不到对应webhook,触发404。 - 若你一开始就用
followup.send()跳过了原始响应的初始化,原始响应的webhook关联根本不存在,也会导致此错误。
实现按钮编辑原始嵌入的正确方案
方案1:直接发送原始响应(推荐,无需defer时用)
如果不需要处理耗时操作,直接用interaction.response.send_message()发送带视图的初始消息,后续按钮回调直接调用edit_original_response()编辑即可:
import discord from discord import appcommands class HelpButtonView(discord.ui.View): def __init__(self): super().__init__(timeout=None) # 可选:设置timeout=None让视图永久有效 @discord.ui.button(label="切换菜单", style=discord.ButtonStyle.primary) async def switch_menu(self, interaction: discord.Interaction, button: discord.ui.Button): # 创建新的嵌入内容 new_embed = discord.Embed(title="新菜单页面", description="点击按钮切换后的内容", color=discord.Color.blue()) # 编辑原始响应消息 await interaction.edit_original_response(embed=new_embed, view=self) @appcommands.command(name="help", description="显示帮助菜单") async def help_cmd(interaction: discord.Interaction): # 初始嵌入 initial_embed = discord.Embed(title="初始帮助菜单", description="点击按钮切换页面", color=discord.Color.green()) # 直接发送原始响应,绑定视图 await interaction.response.send_message(embed=initial_embed, view=HelpButtonView())
方案2:必须使用defer时(处理耗时操作)
如果需要先defer(比如加载数据等耗时操作),则需要保存followup.send()返回的消息对象,在按钮回调中编辑该消息:
import discord import asyncio from discord import appcommands class HelpButtonView(discord.ui.View): def __init__(self, target_msg): super().__init__(timeout=None) self.target_msg = target_msg # 保存followup消息对象 @discord.ui.button(label="切换菜单", style=discord.ButtonStyle.primary) async def switch_menu(self, interaction: discord.Interaction, button: discord.ui.Button): new_embed = discord.Embed(title="新菜单页面", description="defer后切换的内容", color=discord.Color.purple()) # 编辑保存的followup消息 await self.target_msg.edit(embed=new_embed, view=self) @appcommands.command(name="help", description="显示帮助菜单") async def help_cmd(interaction: discord.Interaction): await interaction.response.defer() # 触发加载状态 # 模拟耗时操作:比如从数据库拉取帮助数据 await asyncio.sleep(2) # 发送followup消息并保存对象 initial_embed = discord.Embed(title="初始帮助菜单", description="defer加载后的初始页面", color=discord.Color.orange()) followup_msg = await interaction.followup.send(embed=initial_embed, view=None) # 重新绑定带消息对象的视图 await followup_msg.edit(view=HelpButtonView(followup_msg))
额外注意事项
- 若设置了视图的
timeout,超时后视图会失效,按钮点击会触发其他错误;若需要永久有效视图,设置timeout=None并在机器人启动时添加bot.add_view(HelpButtonView())(针对方案1)。 - 确保机器人拥有编辑消息的权限,虽然此错误主要不是权限问题,但权限不足会引发其他异常。
内容的提问来源于stack exchange,提问作者Skillz Gamin
相关产品推荐
相关产品推荐

