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

使用discord.py发送带单图的Discord嵌入失败,求排查原因

问题原因及修复方案

核心错误点

  • embed.set_image()参数错误:该方法要求传入图片的URL,不能直接传本地文件名output.png。如果用本地图片,需要先上传文件,再引用附件的URL格式attachment://文件名。
  • ctx.send()的file参数错误:这里需要传入discord.File实例,而不是字符串文件名,直接传字符串会导致发送失败。

修复后的代码

@client.command(name='test')
async def test(ctx):
    # 创建File对象,指定本地图片文件
    file = discord.File("output.png", filename="output.png")
    # 创建嵌入消息
    embed = discord.Embed(title='test', colour=discord.Colour.dark_orange())
    # 使用附件URL设置嵌入图片
    embed.set_image(url="attachment://output.png")
    # 发送嵌入和文件
    await ctx.send(embed=embed, file=file)

补充说明

  • 上传文件后,Discord会生成临时的附件URL,用attachment://文件名的格式可以让嵌入消息正确引用这个上传的图片。
  • 必须确保output.png确实在项目根目录,或者使用完整的文件路径(比如./output.png)来避免路径问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 07:25:33