Discord.js 消息的DiscordJs级联
我是discordjs软件包的新手,我一直在四处寻找,但在过去的两天里,我一直被困在这个问题上 我试图实现的是“问卷调查”,它将引导用户回答一些问题。例如: 用户类型:!阿斯科特Discord.js 消息的DiscordJs级联,discord.js,Discord.js,我是discordjs软件包的新手,我一直在四处寻找,但在过去的两天里,我一直被困在这个问题上 我试图实现的是“问卷调查”,它将引导用户回答一些问题。例如: 用户类型:!阿斯科特 问:你想要红葡萄酒还是白葡萄酒 白葡萄酒 问:你想要白葡萄酒。你喜欢verdejo还是rueda A:鲁埃达 问:这是鲁埃达。你想要室温还是冷藏 A:室温 我得到的是: msg.author.send('Do you want red wine or white wine?'); m
- 问:你想要红葡萄酒还是白葡萄酒
- 白葡萄酒
- 问:你想要白葡萄酒。你喜欢verdejo还是rueda
- A:鲁埃达
- 问:这是鲁埃达。你想要室温还是冷藏
- A:室温
msg.author.send('Do you want red wine or white wine?');
msg.author.send(WineText)
.then((systemMsg) => {
systemMsg.channel.awaitMessages(response => response.content, {
max: 1,
time: 5000,
errors: ['time'],
}).then((collected) => {
systemMsg.channel.send(`You want ${collected.first().content}. Do you prefer a verdejo or a rueda?`);
collected.channel.awaitMessages(response => response.content, {
max: 1,
time: 10000
});
})then((collected2) => {
systemMsg.channel.send(`A ${collected2.first().content} it's. Do you want it at room temperature or chill? `);
}).catch((err) => {
return;
});
但它只是在提出第二个问题后停止,而不是捕获/收集第二个答案。我做错了什么
非常感谢你的回答 我要做的是将所有的问题(和答案)放在一个数组中,将这些语句包装在一个函数中,然后在如下迭代中循环它们:
let i = 0; let questions = ['Q1', 'Q2'];
function askQuestion() {
const filter = m => m.author.id == message.author.id;
message.channel.send(questions[i]).then(() => {
message.channel.awaitMessages(filter, { max: 1, time: 120000, errors: ['time'] })
.then(async collected => {
//do stuff
message.channel.send(`answer was ${collected.first().content}`);
i++; //increase iteration
if (i === questions.length) return;
else askQuestion();
})
.catch(collected => {
//timed out
});
});
}
askQuestion();
它有助于稍微清理代码。您可以根据自己的需要进行更改
我制作了一个机器人,它可以像这样提问,你可以查看代码你的事件结构错误:
msg.author.send('Do you want red wine or white wine?');
msg.author.send(WineText)
.then((systemMsg) => {
systemMsg.channel.awaitMessages(response => response.content, {
max: 1,
time: 5000,
errors: ['time']
}).then((collected) => {
systemMsg.channel.send(`You want ${collected.first().content}. Do you prefer a verdejo or a rueda?`);
collected.first().channel.awaitMessages(response => response.content, {
max: 1,
time: 10000
}).then((collected2) => {
systemMsg.channel.send(`A ${collected2.first().content} it's. Do you want it at room temperature or chill? `);
}).catch((err) => {
return;
});
});
});
您犯了两个关键错误:
(1) 您向收集的频道发送了一条消息。但是,collected
是消息的集合,没有通道属性。要将其发送到频道,只需执行collected.first().channel
,因为collected.first()
是一条消息。
(2) 您在代码末尾缺少了两个}
s,因为您包含了then
s且从未关闭