简体   繁体   English

Discord.js V13中如何编辑embed字段值

[英]How to edit embed field value in Discord.js V13

So I am trying to edit one field in an embed.所以我试图在嵌入中编辑一个字段。 The fetched embed looks like this:获取的嵌入看起来像这样:

MessageEmbed {
  type: 'rich',
  title: null,
  description: null,
  url: null,
  color: 5763719,
  timestamp: null,
  fields: [
    { name: 'Suggestie', value: 'test1', inline: false },
    { name: 'Status', value: 'Open', inline: true },
    { name: 'ID', value: 'SPogb', inline: true },
    { name: 'User ID', value: '291272018773671937', inline: true },
    {
      name: 'Opmerking van staff:',
      value: 'Geen opmerking',
      inline: false
    }
  ],
  thumbnail: null,
  image: null,
  video: null,
  author: {
    name: 'Nigel#7777',
    url: undefined,
    iconURL: 'https://cdn.discordapp.com/avatars/291272018773671937/b2637472f4502b2b2280b6302d3f666c.webp',
    proxyIconURL: 'https://images-ext-1.discordapp.net/external/I_MfeMd6YbK_NxsY_kG-k7P6hiytpXx3tNk9ZJdd9lg/https/cdn.discordapp.com/avatars/291272018773671937/b2637472f4502b2b2280b6302d3f666c.webp'
  },
  provider: null,
  footer: null
}

I am trying to edit the Status field value from Open to Approved, but I have no idea how to achieve this.我正在尝试将 Status 字段值从 Open 编辑为 Approved,但我不知道如何实现。 I currently have:我目前有:

const kanaal = interaction.guild.channels.cache.get(config.kanalen.suggestieKanaal)
        const ID = interaction.options.getString('id');
        let correcteSuggest;
        kanaal.messages.fetch({limit: 50}).then(messages => {
            const botMessages = messages.filter(msg => msg.author.bot);
            botMessages.forEach(msg => {
                if(msg.embeds[0].fields[2].value === interaction.options.getString('id')) {
                    correcteSuggest = msg;
                }
            })
            console.log(correcteSuggest.embeds[0].fields)
            // correcteSuggest.edit(embeds[0].fields[1] = ({name: "Status", value: "Approved", inline: true}))
            // const editedEmbed = new Discord.MessageEmbed(correcteSuggest.embeds[0]).fields[3]



        })

The commented stuff I tried but did not seem to work.我尝试过但似乎没有用的评论内容。 I have seen other people get the embed and do like.setDescription, but I don't know how that would work with.setField since there are more then one fields.我已经看到其他人获得嵌入并执行 like.setDescription,但我不知道这将如何与 .setField 一起使用,因为有不止一个字段。

Fixed this by doing:通过执行以下操作解决此问题:

correcteSuggest.embeds[0].fields.find(f => f.name === "Status").value = "Approved";

correcteSuggest.edit({embeds: [correcteSuggest.embeds[0]]})

your logic of editing the embed is right but the way is wrong.您编辑嵌入的逻辑是正确的,但方式是错误的。 Discord.JS doesn't actually just update one field, it updates the whole message so we'll need to change the way to a whole new MessageEmbed. Discord.JS 实际上不只更新一个字段,它更新整个消息,因此我们需要将方式更改为全新的 MessageEmbed。

const kanaal = interaction.guild.channels.cache.get(config.kanalen.suggestieKanaal)
const ID = interaction.options.getString('id');
let correcteSuggest;
kanaal.messages.fetch({
    limit: 50
}).then(messages => {
    const botMessages = messages.filter(msg => msg.author.bot);
    botMessages.forEach(msg => {
        if (msg.embeds[0].fields[2].value === interaction.options.getString('id')) {
            correcteSuggest = msg;
        }
    })
    if (correcteSuggest.embeds[0]) {
        const editedEmbed = new Discord.MessageEmbed(correcteSuggest.embeds[0]);
        editedEmbed.fields[1].value = "Approved";
        correcteSuggest.edit({
            embeds: [editedEmbed]
        });
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM