【发布时间】: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