【问题标题】:How to await promise.all when editing message with embed?使用嵌入编辑消息时如何等待 promise.all?
【发布时间】:2021-11-01 06:42:06
【问题描述】:

我有下一个代码:

let usersThatReacted = [];
  fetchMsg.reactions.cache.map(async (reaction) => {
    if (reaction.emoji.name !== '????') return;
    let reactedUsers = await reaction.users.fetch();
    reactedUsers.map((user) => {
      usersThatReacted.push(db.get(user.id));
    });
    message.channel.send(await (Promise.all(usersThatReacted))); //here i have message from bot with users..
  });

而且它工作得很好,机器人等待(“等待”)直到数据库中的所有请求都完成,然后它向对消息做出反应的用户数组发送一条消息..

但是如何用嵌入的编辑消息的命令做同样的事情呢?

我的意思是:

let usersThatReacted = [];
    message.reactions.cache.map(async (reaction) => {
      if (reaction.emoji.name !== '????') return;
      let reactedUsers = await reaction.users.fetch();
      reactedUsers.map((user) => {
        usersThatReacted.push(db.get(user.id));
      });
     
    });

message.channel.messages.fetch(message.id).then(message => {
  const exampleEmbed = new Discord.MessageEmbed()
    .setColor('#0099ff')
    .setAuthor('.: Отметки активности :.', 'https://emojigraph.org/media/microsoft/crossed-swords_2694-fe0f.png')
    .addFields(
      { name: '????', value: `${usersThatReacted}`, inline: true }, //but here code dont waiting until "usersThatReacted" (array) will be complete(
    )
  message.edit(exampleEmbed)
    .then(msg => console.log(`Updated the content of a message to ${msg.content}`))
    .catch(console.error);
})

当我删除message.channel.send(await (Promise.all(usersThatReacted)));

我收到错误“RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty.”,这意味着这部分代码(带有发送编辑消息)不要等到数组完成..

当使用 value: `${(await (Promise.all(usersThatReacted)))}`, 而不是 value: `${usersThatReacted}`, 我有错误:等待未定义

【问题讨论】:

  • 发生了什么?有什么错误/问题吗?
  • 是的,已经在编辑帖子了。我已经尝试过“message.edit(await (Promise.all(exampleEmbed)))”但同样的错误(
  • "而且效果很好" - 不,它没有。如果您正在映射的 message.reactions.cache 中有多个 reactions(我不知道不和谐 - 缓存是一个数组吗?),它将向 message.channel 发送多个不可预测数量的 usersThatReacted .
  • 对,完美)我有类似的东西:????设置迈克,约翰,登,罗斯。然后我想看看“谁对消息做出了反应??????”我输入一些命令,例如:“!who”,然后机器人发送给我:Mike John Den Rose。没错,它是数组)

标签: javascript node.js discord.js


【解决方案1】:

在这里,您无需等待承诺解决。

addFields({ 
    name: '?', 
    value: `${usersThatReacted}`, 
    inline: true 
})

你需要在那里使用Promise.all

addFields({ 
    name: '?', 
    value: `${(await (Promise.all(usersThatReacted)))}`, 
    inline: true 
})

【讨论】:

  • 已经试过了,但是在模板表达式中有错误:/home/index.js:153 { name: '?', value: ${await Promise.all(usersThatReacted)}, inline: true }, SyntaxError: Missing }。并在“等待”字上设置标记(^^^^)
  • 现在(新的“(”和“)”有错误:“等待未定义”
  • 我已经尝试过插入新变量(例如:let test = await Promise.all(usersThatReacted),但在嵌入中我有同样的错误)
  • 用最后一次尝试和错误编辑帖子
猜你喜欢
  • 2020-03-17
  • 2019-10-15
  • 2021-05-14
  • 2018-12-02
  • 2021-08-31
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2020-09-08
相关资源
最近更新 更多