嵌入消息未更新

Mis*_*qua 3 javascript node.js discord.js

我想用嵌入的消息进行表决。
当有人添加反应时,我想添加一个喜欢并显示嵌入中的喜欢数量。这里是一个例子:
例

每当有人点击like时,我所有的代码行都会工作,而我最终将所链接的Field值更改为:

messageReaction.message.embeds[0].fields[0] = "Some much like";
Run Code Online (Sandbox Code Playgroud)

但是嵌入消息不会更新。
我尝试使用以下方法更新消息:

function doAfakeEdit(message){
  message.edit(message.content);
}
Run Code Online (Sandbox Code Playgroud)

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

omn*_*nom 12

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

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

message.embeds[0].fields[0] = "Some much like";
message.edit(new Discord.RichEmbed(message.embeds[0]));
Run Code Online (Sandbox Code Playgroud)


Blu*_*her 5

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

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

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'
  }]
});
Run Code Online (Sandbox Code Playgroud)

2)发送Embed到您的频道(我在其中添加了一些Reaction内容-可能与您使用的方式相同):

// 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);
Run Code Online (Sandbox Code Playgroud)

3)ReactionCollector在我放置的地方创建一个内部// fun stuff here(可以使用不同的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`));
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

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);
Run Code Online (Sandbox Code Playgroud)

对于我的代码,仅在?时进行编辑。按下表情符号只是为了好玩。如果您需要编辑以上代码的帮助,请告诉我。希望能帮助到你。

  • 嗯,这只是“编辑新嵌入的消息”,这是一条很长的消息,但是它起作用了:)谢谢; (2认同)