嵌入消息不会更新

问题描述 投票:2回答:2

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

每当有人点击时,我的所有代码行都会起作用,我最终将链接的字段值更改为:

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

但嵌入消息不会更新。 我试图用这个来更新消息:

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

它仍然保留了该领域的旧价值。 我该怎么办?

javascript node.js discord.js
2个回答
5
投票

我想知道你的问题是你要么重新使用变量名,要么将旧数据放回编辑过的消息中,要么是别的。无论如何,这里有一些对我有用的东西:

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)在我放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`));

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);

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


0
投票

答案很晚。但万一有人发现这一点。这是一个短得多的方式。

如果你有大型嵌入并且不想重建整个嵌入,那么更有用:

message.embeds[0].fields[0] = "Some much like;
message.edit(new Discord.RichEmbed(message.embeds[0]));
© www.soinside.com 2019 - 2024. All rights reserved.