簡體   English   中英

如何讓discord.js bot在用戶響應后才回復一系列DM消息?

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

我正在嘗試讓機器人讀取和回復 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收集的awaitMessages ,即回復skip將告訴第一部分沒有圖像並同時告訴第二部分輸入的日期被skip

有沒有辦法讓機器人只在用戶響應前一個功能后才執行一個功能? 我嘗試使用setTimeout()來延遲每個部分,但這不是理想的功能。 我希望機器人在讀取用戶輸入后立即回復。

我該如何解決問題?

您需要將第二個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");
          });
      });
  });

是的,這看起來很糟糕。 您可以查看 async/await 以使您的代碼更易於閱讀並減少縮進。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM