【问题标题】:How to get discord.js bot to reply to a series of DM messages only after user response?如何让 discord.js 机器人仅在用户响应后回复一系列 DM 消息?
【发布时间】:2019-12-16 21:04:10
【问题描述】:

我正在尝试让机器人阅读和回复来自 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 来收集用户对每个问题的回答。预期的功能是机器人将存储响应并在主服务器上创建包含所有信息的嵌入式帖子。但是现在,每个问题都是在同一时间提出的,而不是在等待用户回复之后。这是我当前代码的 sn-p:

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() 来延迟每个部分,但这不是一个理想的功能。我希望机器人在读取用户输入后立即回复。

我该如何解决这个问题?

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案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");
              });
          });
      });
    

    是的,这看起来很糟糕。您可以查看 async/await 以使您的代码更易于阅读并减少缩进。

    【讨论】:

    • 谢谢,这对我帮助很大,对我有用。我之前尝试过使用 async/await 结构,但无法使其正常工作。使用 then() 和 catch() 确实会使代码非常混乱,所以我一定会考虑正确使用 async/await。
    猜你喜欢
    • 2020-11-26
    • 2020-09-16
    • 2021-06-26
    • 2022-01-24
    • 1970-01-01
    • 2021-08-06
    • 2021-07-19
    • 1970-01-01
    • 2019-11-23
    相关资源
    最近更新 更多