Embed message doesn't update

纵饮孤独 提交于 2019-12-19 09:56:40

问题


I want to make a vote with an embed message.
When someone adds a reaction, I want to add a like and to show the number of likes in the embed. Here an example:

Whenever someone clicks on like, all my code lines work and I finally change the Field value linked to like just like that :

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

But the embed message doesn't update.
I've tried to update the message with this:

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

It still keeps the old value of the field.
What should I do?


回答1:


I wonder if your problem is that you're either re-using variable names, putting the old data back into the edited message, or something else. Anyway, here's something that worked for me:

1) Create an Embed to send to the user (I assume you already did this, creating the Embed you showed on 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'
  }]
});

2) Send Embed to your channel (I added some Reactions to it - possibly the same way as you):

// 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) Create a ReactionCollector inside where I put // fun stuff here (you can use a different reactionFilter and time limit):

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) In the 'collect' event (where I put // see step 4), create a new Embed with mostly similar values (or not - you change whatever you want), then put that new Embed back into the original message via .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

So the whole thing ends up looking something like this:

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

For my code, edits are only made when the ✅ emoji is pressed, just for fun. Please let me know if you need help editing the code above. Hope it helps.




回答2:


Very late answer. But just in case someone finds this. Theres a much shorter way.

And more useful if you have large embeds and don't want to rebuild your whole embed:

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


来源:https://stackoverflow.com/questions/50668366/embed-message-doesnt-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!