我该如何保证,这样我才能在此区域中使用sendMessage。 DISCORD.JS

时间:2019-06-18 00:12:09

标签: node.js discord.js

如何用sendMessage替换[机器人发送的消息]?

client.guilds.get("588125693225992195")
  .channels
  .find(ch => ch.name === 'order-requests')
  .send(richemb)
  .then(sentMessage => sentMessage.react('?'))
  .catch(() => console.error('Failed to react.'))

const filter = (reaction) => reaction.emoji.name === '?'

message.awaitReactions(filter)
  .then([themessagethebotsent].delete(0500))
  .catch(console.error);```

1 个答案:

答案 0 :(得分:0)

sentMessage的{​​{3}}在then()回调中。这意味着无法在该回调之外访问它。

您有两个主要解决方案。保持当前设置,您可以将需要sentMessage的代码放入回调中。或者,您可以使用关键字scope获得更好的流程。请注意,它只能在await中使用。

示例1:

const guild = client.guilds.get('588125693225992195');
const channel = guild.channels.find(ch => ch.name == 'order-requests');

channel.send(richemb)
  .then(sentMessage => {
    sentMessage.react('?');

    message.awaitReactions(reaction => reaction.emoji.name === '?')
      .then(() => sentMessage.delete(0500));
  })
  .catch(console.error);

示例2:

const guild = client.guilds.get('588125693225992195');
const channel = guild.channels.find(ch => ch.name === 'order-requests');

try {
  const sentMessage = await channel.send(richemb);
  await sentMessage.react('?');

  await sentMessage.awaitReactions(reaction => reaction.emoji.name === '?');
  await sentMessage.delete(0500);
} catch(err) {
  console.error(err);
}