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

使用discord.py编辑交互消息添加图片时遇TypeError问题求助

解决discord.py中InteractionResponse.edit_message()无法传file参数的问题

问题原因

ctx.response.edit_message() 方法本身不支持 file 和 discord.Attachment 参数,这是discord.py交互响应API的设计限制——交互响应的编辑接口仅支持更新内容、嵌入、组件等,不直接处理文件附件的上传替换。

解决方案

方案1:通过原始消息对象的edit()方法更新

发送初始响应后,你可以通过ctx.original_response()获取到已发送的消息对象,这个对象的edit()方法支持file参数,能正常替换图片:

# 获取初始发送的响应消息对象
original_message = await ctx.original_response()

# 使用消息对象的edit方法更新内容和图片
await original_message.edit(
    embed=embed,
    file=discord.File(f"image/cartes/{collection}/{id_carte}.png"),
    ephemeral=True
)

方案2:通过Embed图片URL替换(无需重新上传)

如果你的图片已经上传到可访问的地址(比如Discord自动生成的附件URL),可以直接修改Embed的图片URL,不用上传新文件:

# 更新Embed中的图片链接
embed.set_image(url="https://example.com/your-new-image.png")

# 直接用ctx.response.edit_message()更新Embed即可
await ctx.response.edit_message(embed=embed, ephemeral=True)

注意事项

  • 若使用方案1,要确保在交互的有效期内调用ctx.original_response()(Discord交互默认有效期15分钟)。
  • 重复上传相同图片时,优先用方案2的URL方式,避免冗余的文件上传请求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 10:56:23