我正在尝试让机器人阅读和回复 DM channel 中用户的一系列回复。在我的设置中,工作流的过程是(左边的消息作者):
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 channel 中向机器人发送消息 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
将告诉第一部分没有图像并告诉第二部分同时将输入日期的部分跳过
。
有没有办法让机器人只在用户响应前一个功能后才执行功能?我试过使用 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 以使您的代码更易于阅读并减少缩进。
关于javascript - 如何让 discord.js 机器人仅在用户响应后才回复一系列 DM 消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57424675/