如何让discord.js机器人仅在用户响应后才能回复一系列DM消息?

时间:2019-08-09 06:42:51

标签: javascript node.js discord.js

我正在尝试让一个机器人读取并回复DM频道中用户的一系列回复。在我的设置中,工作流程为(消息作者在左侧):

user: "bot post"
bot: "Please upload an image if necessary"
user: *uploads image*
bot: "Now please set a date in dd/mm/yyyy format"
user: "09/23/1952"
bot: "Now please set some contact info"
user: ...

现在我有一个awaitMessages来收集用户对每个问题的回答。预期的功能是,机器人将存储响应并在主服务器上创建包含所有信息的嵌入式帖子。但是现在,每个问题都在同一时间被问到,而不是在等待用户答复之后才问。这是我当前代码的一小段:

client.on("message", msg => {
    var words = msg.content.split(' ').map(x => x.toLowerCase());
    if (msg.author.bot || msg.guild !== null || words[1] !== 'post') return;
    const filter = m => m.author.id === msg.author.id;

    img = "" \\ store the image url inputted
    date = "" \\ store the date as a string

    \\ bot asks for image
    msg.author.send("First, upload an image if you have one. If not, just reply `skip`.") 
        .then(() => {
            msg.channel.awaitMessages(filter, {
                max: 1,
                time: 300000
            })
                .then((collected) => {
                    console.log(collected.first().content)
                    if (collected.first().attachments.size === 1) {
                        img = collected.first().attachments.first().url;
                        msg.author.send("Image successfully uploaded.");
                    } else {
                        msg.author.send("No image uploaded.");
                    }
                })
                .catch(() => {
                    msg.author.send("No image uploaded. Your time limit ran out");
                });
        })

    \\ bot asks for date input
    msg.author.send("Next, please input a start/due date in `dd/mm/yyyy`.") 
        .then(() => {
            msg.channel.awaitMessages(filter, {
                max: 1,
                time: 30000,
                errors: ['time'],
            })
                .then((collected) => {
                    date = collected.first().content
                    msg.author.send("Date has been entered.")
                })
                .catch(() => {
                    msg.author.send("No date was entered. Your time limit ran out");
                });
        });

})

运行机器人并在DM通道中向机器人传递消息bot post会导致以下结果:

bot: First upload an image if you have one. If not just reply skip.
bot: Next, please input a start date/due date in dd/mm/yyyy form.

这将导致下一条写入的消息是同时收集在awaitMessages中的消息,即回复skip将告诉第一部分没有图像,并告诉第二部分输入的日期同时是skip

有什么方法可以让机器人仅在用户响应前一个功能后才执行功能?我尝试使用setTimeout()来延迟每个部分,但这不是理想的功能。我希望该机器人在读取用户输入后立即回复。

我该如何解决问题?

1 个答案:

答案 0 :(得分:1)

您需要将第二个send函数移到第一个函数的then()中。有点像这样:

client.on("message", msg => {
    var words = msg.content.split(" ").map(x => x.toLowerCase());
    if (msg.author.bot || msg.guild !== null || words[1] !== "post") return;
    const filter = m => m.author.id === msg.author.id;

    img = ""; // store the image url inputted
    date = ""; // store the date as a string

    // bot asks for image
    msg.author
      .send("First, upload an image if you have one. If not, just reply `skip`.")
      .then(() => {
        msg.channel
          .awaitMessages(filter, {
            max: 1,
            time: 300000
          })
          .then(collected => {
            console.log(collected.first().content);
            if (collected.first().attachments.size === 1) {
              img = collected.first().attachments.first().url;
              msg.author.send("Image successfully uploaded.");

              // bot asks for date input
              msg.author
                .send("Next, please input a start/due date in `dd/mm/yyyy`.")
                .then(() => {
                  msg.channel
                    .awaitMessages(filter, {
                      max: 1,
                      time: 30000,
                      errors: ["time"]
                    })
                    .then(collected => {
                      date = collected.first().content;
                      msg.author.send("Date has been entered.");
                    })
                    .catch(() => {
                      msg.author.send(
                        "No date was entered. Your time limit ran out"
                      );
                    });
                });
            } else {
              msg.author.send("No image uploaded.");
            }
          })
          .catch(() => {
            msg.author.send("No image uploaded. Your time limit ran out");
          });
      });
  });

是的,这看起来很糟糕。您可以研究异步/等待,以使代码更易于阅读并且缩进更少。