【问题标题】:Problem with discord embeds, and [object Promise]不和谐嵌入问题和 [object Promise]
【发布时间】:2020-09-18 22:57:12
【问题描述】:

我正在编写一个不和谐机器人,我需要知道谁对指定消息做出了反应,并将他们的用户名放入不和谐嵌入中。

代码如下:

MSG.messages.fetch({ around: GameID, limit: 1 }).then((msg) => {
  fetchedMsg = msg.first();
  let MessageOBJ = fetchedMsg.reactions.cache.get("????");

  const embed = new Discord.MessageEmbed()
    .setTitle("All players here !")
    .setDescription("Here's all of the players for this game")
    .addField(
      "Players",
      MessageOBJ.users.fetch().then((users) => {
        users.forEach((element) => {
          `${element.username}\n`;
        });
      })
    );

  fetchedMsg.edit(embed);
});

但是,机器人在嵌入的 Players 类别中显示 [object Promise]

【问题讨论】:

  • 使用Array.prototype.map() 而不是Array.prototype.forEach()
  • 我试过了,但这仍然给了我 [object Promise]
  • 你可以试试console.log(users)吗?
  • 我会,但我是问题

标签: javascript node.js discord discord.js es6-promise


【解决方案1】:

尝试在创建嵌入之前启动承诺。

function promise() {
  return new Promise((resolve, reject) => {
    resolve(['User 1', 'User 2', 'User 3']);
  });
}

console.log(promise().then((result) => result)) // putting the promise inside
promise().then((result) => console.log(result)) // initiating it outside

// also, make sure to use `Array.map` instead of `Array.forEach`
promise().then((result) => console.log(result.forEach((user) => `${user}\n`)))
promise().then((result) => console.log(result.map((user) => `${user}\n`)))
MSG.messages.fetch({ around: GameID, limit: 1 }).then((msg) => {
 fetchedMsg = msg.first();
 let MessageOBJ = fetchedMsg.reactions.cache.get('?');
 MessageOBJ.users.fetch().then((users) => { // initiate promise
 const embed = new Discord.MessageEmbed()
  .setTitle('All players here !')
  .setDescription("Here's all of the players for this game")
  .addField(
   'Players',
    users.map((element) => {
     `${element.username}\n`;
    });
   })
  );
});

【讨论】:

    【解决方案2】:

    我不是 discord api 方面的专家,但是您遇到的问题可能是 promise 的问题。该方法返回一个承诺,其中可能包含信息。

    有几种方法可以解决这个问题。我的首选方式是使用await

    const embed = new Discord.MessageEmbed()
        .setTitle('All players here !')
        .setDescription('Here\'s all of the players for this game')
        .addField('Players', await MessageOBJ.users.fetch().then(users => {
            users.map(element => 
                `${element.username}\n`
        )
        }))
    

    如您所见,addField 需要实际数据,而不是返回数据的承诺。

    要使其正常工作,您可能必须将您的函数标记为 async

    此外,您的一些内联函数看起来也有错误的括号格式来实现您想要实现的目标。

    您所追求的“最终结果”可能是这样的:

    MSG.messages.fetch({ around: GameID, limit: 1 })
        .then(async msg => {
            const fetchedMsg = msg.first();
            const MessageOBJ = fetchedMsg.reactions.cache.get("?");
            const playerNames = await MessageOBJ.users.fetch()
                .then(users =>
                    users.map(element =>
                        `${element.username}\n`
                    )
                )
            const embed = new Discord.MessageEmbed()
                .setTitle('All players here !')
                .setDescription('Here\'s all of the players for this game')
                .addField('Players', playerNames)
            fetchedMsg.edit(embed);
        });
    

    【讨论】:

    • 我试过了,这给了我“未定义”到不和谐嵌入
    • 这里是一篇关于箭头函数返回规则的随机文章:jaketrent.com/post/javascript-arrow-function-return-rules
    • 那不是我投的反对票。有没有办法在 users.map(element => ...) 中使用“if”并且代码将继续正常工作?
    • 是的,但是你必须使用 return。例如users.map(user => {if(user.something){return 'something'}else{return 'something else'}})
    猜你喜欢
    • 1970-01-01
    • 2020-12-25
    • 1970-01-01
    • 2021-12-03
    • 2021-01-05
    • 1970-01-01
    • 2019-10-11
    • 2020-12-31
    • 2021-05-14
    相关资源
    最近更新 更多