嵌入消息未更新 [英] Embed message doesn't update

查看:51
本文介绍了嵌入消息未更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用嵌入的消息进行投票。

当某人添加反应时,我想添加一个喜欢并显示嵌入中的喜欢数量。下面是一个示例:

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:

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

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?

推荐答案

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

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)创建一个嵌入发送给用户(我假设您已经做到了,创建了在imgr上显示的嵌入):

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)将 Embed 发送到您的频道(我在其中添加了 Reaction -可能与您的操作方式相同:

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)在我所在的位置创建一个 ReactionCollector //有趣的东西放在这里(您可以使用其他的 reactionFilter 和时间限制):

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)在'collect'事件中(在其中放置 //参见步骤4 ),创建一个新的 Embed ,其值基本上类似(或者不-您更改任何内容),然后通过 .edit(...)将新的嵌入放回原始消息中:

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.

这篇关于嵌入消息未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆