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

Discord.js使用interaction.reply如何让附件与Embed分开展示

Discord.js Embed内嵌本地图片问题修复方案

问题原因

  • 当前代码仅将本地图片作为独立附件发送,没有将附件和Embed的图片字段关联,所以图片不会展示在Embed内部,只会单独显示在消息下方
  • 原有随机取图逻辑存在隐患:直接生成数字拼接.jpg作为文件名,和fs.readdir读取到的真实文件名可能不匹配,容易出现文件不存在的报错

修复代码

fs.readdir("./pics", (error, files) => {
  if (error) return console.log(error)
  // 直接从读取到的文件列表中随机选取,避免文件名不匹配问题
  const randomPic = files[Math.floor(Math.random() * files.length)];

  const imagembed = new Discord.MessageEmbed()
    .setTitle("Some cute pic for you!")
    .setColor("#980002")
    .setFooter("Requested by " + interaction.member.user.tag)
    // 通过attachment://前缀关联附件到Embed的图片字段
    .setImage(`attachment://${randomPic}`);
  // 发送时同时传入embed和对应附件
  interaction.reply({ embeds: [imagembed], files: [`./pics/${randomPic}`]})
})

实现说明

当需要在Discord Embed中展示本地上传的图片时,必须使用attachment://[附件文件名]的格式指定Embed的图片地址,Discord会自动将上传的对应附件内嵌到Embed的指定位置,不会再作为独立文件单独展示。
如果你使用的是discord.js v14及以上版本,将MessageEmbed替换为EmbedBuilder即可,其余逻辑完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 05:36:04