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

Discord Embed 如何正确关联本地images目录下的缩略图

解决方案

你当前代码的问题有两个:

  1. 没有将本地图片作为消息附件随Embed一起发送
  2. attachment://协议后需要填写的是附件定义的文件名,不能直接写本地文件路径

修正步骤

  • 首先导入discord的File类用于读取本地文件构造消息附件
  • 构造File对象时指定和Embed中匹配的文件名
  • 发送Embed时同步传入附件对象

完整修正后代码

from discord import File # 顶部导入依赖

# 构造Embed逻辑不变
embed = discord.Embed(title="Game Started", description=body)
embed.add_field(name=captain1, value=', '.join(team_one), inline=False)
embed.add_field(name=captain2, value=', '.join(team_two))

# 构造本地图片附件,第二个参数为自定义的附件文件名
thumbnail_file = File("images/elite.png", filename="elite.png")
# 这里直接跟自定义的附件文件名即可,不需要加本地目录
embed.set_thumbnail(url="attachment://elite.png")

# 发送时传入file参数挂载附件
await ctx.send(embed=embed, file=thumbnail_file)

补充注意事项

  • 确认本地images/elite.png路径正确,相对路径是相对于你运行Python脚本的工作目录,路径不确定可以改用绝对路径避免读取失败
  • attachment://后填写的文件名必须和File构造时的filename参数完全一致,大小写也需要匹配
  • 如果需要同时发送多个附件,使用files=[file1, file2]参数传入附件列表即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.06 13:48:04