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

如何在Discord.js中获取嵌入消息的fields数组并编辑字段?

问题

我通过以下代码获取交互中的嵌入消息:

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isButton()) return;
    const raidembed = interaction.message.embeds
    console.log(raidembed)
})

返回结果如下:

[
  Embed {
    data: {
      type: 'rich',
      title: 'RAID DU : A :',
      image: [Object],
      footer: [Object],
      fields: [Array],
      description: '# Caveau des Incarnations\n### <:skull:1193157255407882340> Normal (:)',
      color: 9124820
    }
  }
]

我需要编辑该嵌入消息的fields,目前使用embed.spliceFields()方法,但尝试console.log(raidembed.fields)或console.log(raidembed.data.fields)均返回undefined,想了解如何获取嵌入数组中的fields数组?

解答

从返回结果能看到,raidembed是一个数组,里面才是单个Embed对象。你直接访问数组的fields或data.fields属性自然会返回undefined,因为数组本身没有这些属性。

正确的操作步骤:

  1. 先取出数组里的第一个Embed实例
  2. 再访问该实例的data.fields获取字段数组,或者直接调用实例的spliceFields()方法修改字段

示例代码:

client.on('interactionCreate', async (interaction) => {
    if (!interaction.isButton()) return;
    const raidembeds = interaction.message.embeds;
    // 取出第一个Embed对象
    const targetEmbed = raidembeds[0];
    
    // 获取fields数组
    const embedFields = targetEmbed.data.fields;
    console.log(embedFields);
    
    // 使用spliceFields修改字段(示例:删除索引0的1个字段,添加新字段)
    targetEmbed.spliceFields(0, 1, { 
        name: '更新后的字段名', 
        value: '更新后的字段内容', 
        inline: false 
    });
    
    // 编辑消息更新嵌入
    await interaction.message.edit({ embeds: [targetEmbed] });
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 14:47:11