【问题标题】:Embed message doesn't update嵌入消息不更新
【发布时间】:2018-11-13 01:37:54
【问题描述】:

我想通过嵌入消息进行投票。
当有人添加反应时,我想添加一个喜欢并在嵌入中显示喜欢的数量。这里举个例子:

每当有人点击喜欢时,我的所有代码行都可以正常工作,我最终像这样更改链接到的字段值:

messageReaction.message.embeds[0].fields[0] = "Some much like";

但是嵌入消息没有更新。
我试图用这个来更新消息:

function doAfakeEdit(message){
  message.edit(message.content);
}

它仍然保留该字段的旧值。
我该怎么办?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    答案很晚。但以防万一有人发现这个。还有更短的方法。

    如果您有大型嵌入并且不想重建整个嵌入,则更有用:

    message.embeds[0].fields[0] = "Some much like";
    message.edit(new Discord.RichEmbed(message.embeds[0]));
    

    【讨论】:

    • 这比目前的答案还要好
    【解决方案2】:

    我想知道您的问题是否在于您正在重用变量名、将旧数据放回已编辑的消息中,或者是其他原因。无论如何,这对我有用:

    1) 创建一个Embed 发送给用户(我假设您已经这样做了,创建了您在imgr 上显示的Embed):

    const embed = new Discord.RichEmbed({
      title: 'Suggestion by someone',
      description: 'This is a test suggestion. Can you please like it or dislike it :)',
      fields: [{
        name: 'Like:',
        value: '<3'
      }]
    });
    

    2) 将Embed 发送到您的频道(我添加了一些Reactions - 可能和您一样):

    // add reaction emojis to message
    message.channel.send(embed)
      .then(msg => msg.react('✅'))
      .then(mReaction => mReaction.message.react('❎'))
      .then(mReaction => {
        // fun stuff here
      })
      .catch(console.log);
    

    3) 在我放置// fun stuff here 的地方创建一个ReactionCollector(您可以使用不同的reactionFilter 和时间限制):

    const reactionFilter = (reaction, user) => reaction.emoji.name === '✅';
    
    // createReactionCollector - responds on each react, AND again at the end.
    const collector = mReaction.message
      .createReactionCollector(reactionFilter, {
        time: 15000
      });
    
    // set collector events
    collector.on('collect', r => {
      // see step 4
    });
    // you can put anything you want here
    collector.on('end', collected => console.log(`Collected ${collected.size} reactions`));
    

    4) 在'collect' 事件中(我放// see step 4 的地方),创建一个新的Embed,其值大多相似(或者不 - 你可以更改任何你想要的),然后将新的Embed 放回来自.edit(...)的原始消息:

    // immutably copy embed's 'Like:' field to new obj
    let embedLikeField = Object.assign({}, embed.fields[0]);
    
    // update 'field' with new value - you probably want emojis here
    embedLikeField.value = '<3 <3 <3';
    
    // create new embed with old title & description, new field
    const newEmbed = new Discord.RichEmbed({
      title: embed.title,
      description: embed.description,
      fields: [embedLikeField]
    });
    
    // edit message with new embed
    // NOTE: can only edit messages you author
    r.message.edit(newEmbed)
      .then(newMsg => console.log(`new embed added`)) // this is not necessary
      .catch(console.log); // useful for catching errors
    

    所以整个事情最终看起来像这样:

    const reactionFilter = (reaction, user) => reaction.emoji.name === '✅';
    
    const embed = new Discord.RichEmbed({
      title: 'Suggestion by someone',
      description: 'This is a test suggestion. Can you please like it or dislike it :)',
      fields: [{
        name: 'Like:',
        value: '<3'
      }]
    });
    
    // add reaction emoji to message
    message.channel.send(embed)
      .then(msg => msg.react('✅'))
      .then(mReaction => mReaction.message.react('❎'))
      .then(mReaction => {
        // createReactionCollector - responds on each react, AND again at the end.
        const collector = mReaction.message
          .createReactionCollector(reactionFilter, {
            time: 15000
          });
    
        // set collector events
        collector.on('collect', r => {
          // immutably copy embed's Like field to new obj
          let embedLikeField = Object.assign({}, embed.fields[0]);
    
          // update 'field' with new value
          embedLikeField.value = '<3 <3 <3';
    
          // create new embed with old title & description, new field
          const newEmbed = new Discord.RichEmbed({
            title: embed.title,
            description: embed.description,
            fields: [embedLikeField]
          });
    
          // edit message with new embed
          // NOTE: can only edit messages you author
          r.message.edit(newEmbed)
            .then(newMsg => console.log(`new embed added`))
            .catch(console.log);
        });
        collector.on('end', collected => console.log(`Collected ${collected.size} reactions`));
      })
      .catch(console.log);
    

    对于我的代码,只有在按下 ✅ 表情符号时才会进行编辑,只是为了好玩。如果您需要帮助编辑上面的代码,请告诉我。希望对您有所帮助。

    【讨论】:

    • 嗯,这是一个相当长的消息,只是“使用新嵌入编辑消息”,但它有效:) 谢谢;)
    猜你喜欢
    • 2021-10-16
    • 2021-12-03
    • 2019-10-11
    • 2021-08-22
    • 2019-08-18
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2018-12-04
    相关资源
    最近更新 更多