【问题标题】:Proper way to edit an embed field编辑嵌入字段的正确方法
【发布时间】:2021-02-04 17:35:23
【问题描述】:

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

这是我的代码的 sn-p 供参考

   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

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

感谢您提供的任何帮助!

【问题讨论】:

  • 你需要创建一个嵌入对象,在频道中发送,当你得到一个反应时修改嵌入对象,然后用新修改的嵌入编辑消息。

标签: javascript discord discord.js


【解决方案1】:

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

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

在您放置// 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);

【讨论】:

  • 这可以与多个嵌入一起使用吗?比如说,如果我之前发送了 3 个嵌入并且第一个有反应,那么我更新那个。然后也许接下来第三个有反应,所以更新了一个,等等。
  • 我相信它可以满足您的需求。但是你应该添加一些东西来停止反应的收集,因为它可以叠加
【解决方案2】:

这很容易做到!您可以执行以下操作:

var Msg = await message.channel.send(eventEmbed)
Let x = new Discord.MessageEmbed()
//code here
Msg.edit(x)

您还应该记住异步消息

【讨论】:

    猜你喜欢
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-18
    • 2021-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多