首页 > 解决方案 > 编辑嵌入字段的正确方法

问题描述

我想知道他们是否是一种编辑已发送的嵌入字段的方法。我想用参加嵌入活动的人员列表更新一个名为参与者的字段。有没有办法访问之前发送的嵌入来编辑这个字段,或者我只需要构建一个新的嵌入,然后使用 .edit 方法就可以了。我还注意到我可以使用这样的行const receivedEmbed = message.embeds[0];来访问嵌入在它所在的任何索引处,但是文档说这可能会混淆缓存(文档参考)。

这是我的代码片段供参考

   var reactions = ['','','' ]; // Valid reactions for filter
    var participants = []; // People attending the event
    // Sending the embed back and then . . .
    message.channel.send(eventEmbed)
    .then(embedMessage => {
        // Adding the reactions after the embed has been created
        embedMessage.react("");
        embedMessage.react("");
        embedMessage.react("");
        console.log(eventEmbed);
        // Reaction Collector to gather the users attending the event.
        const filter = (reaction, user) => {
            return !user.bot && reactions.includes(reaction.emoji.name);
        };
    
// The reactor is used to identify a new user that will be attending the event,
// and adding them to the list
        rc = new Discord.ReactionCollector(embedMessage, filter);

        rc.on('collect', (reaction, user) => {
            console.log(user + " reacted with a " + reaction);
            attendees.push(user); // Add new user the the attendees list
            console.log(attendees); // Debugging
            reaction.users.remove(user); // Reset reaction count back to 1

// Then here I want to edit the participants field with the new participants list

有什么建议或指示吗?我更多的是寻找有关执行此操作的逻辑的示例或解释,而不是我的代码的解决方案,因为我想尝试自己编写它:)。

我很感激可以提供的任何帮助!

标签: javascriptdiscorddiscord.js

解决方案


很难获取嵌入然后对其进行编辑。但是,有一种方法可以从预先存在的对象中编辑它。

您可以保留您的嵌入对象,当您想要更新某些内容时,转到对象的字段属性,获取您想要的字段,编辑该字段,然后将您的原始消息替换为新消息。

在你放的地方// Then here I want to edit the participants field with the new participants list。你应该把

eventEmbed.fields.find(f => f.name === "attendees").value = attendees;
embedMessage.edit(eventEmbed);

推荐阅读