简体   繁体   English

如何让discord.js bot在用户响应后才回复一系列DM消息?

[英]How to get discord.js bot to reply to a series of DM messages only after user response?

I am trying to get a bot to read and reply a series of replies from a user in a DM channel.我正在尝试让机器人读取和回复 DM 频道中用户的一系列回复。 In my setup, the process of workflow would be (message author on left):在我的设置中,工作流程的流程是(左侧的消息作者):

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: ...

Right now I have an awaitMessages to collect user responses to each question.现在我有一个awaitMessages来收集用户对每个问题的回答。 The intended function is that the bot will store the responses and create an embedded post with all the info on the main server.预期功能是机器人将存储响应并创建一个包含主服务器上所有信息的嵌入式帖子。 But right now, each question is asked at the exact same time, instead of after waiting for the user to reply.但是现在,每个问题都是在完全相同的时间提出的,而不是在等待用户回复之后。 Here is a snippet of my current code:这是我当前代码的片段:

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");
                });
        });

})

Running the bot and messaging bot post in a DM channel to the bot results in this:在 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.

This causes the next message written to be the ones collected in both awaitMessages , ie a reply skip will tell the first portion that there is no image AND tell the second portion that the date inputted in is skip simultaneously.这会导致写入的下一条消息是在两个awaitMessages收集的awaitMessages ,即回复skip将告诉第一部分没有图像并同时告诉第二部分输入的日期被skip

Is there any way to get the bot to only perform a function only after the user has responded to the previous one?有没有办法让机器人只在用户响应前一个功能后才执行一个功能? I've tried using a setTimeout() to delay each portion but that's not an ideal functionality.我尝试使用setTimeout()来延迟每个部分,但这不是理想的功能。 I want the bot to reply as soon as it reads the user's input.我希望机器人在读取用户输入后立即回复。

How do I solve the problem?我该如何解决问题?

You need to move your second send function inside the then() of the first.您需要将第二个send函数移到第一个的then()中。 Kind of like this:有点像这样:

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");
          });
      });
  });

And yes, that looks awful.是的,这看起来很糟糕。 You could look into async/await to make your code easier to read and have fewer indentations.您可以查看 async/await 以使您的代码更易于阅读并减少缩进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM