javascript - 收集器出错 | 不和谐.js
问题描述
我希望人们能够对消息做出反应,然后机器人会通过 3 个问题与他们联系。问题是它自己跳过所有问题并自己回答。
如果您能很好地向我描述问题,以便不再发生,我会很高兴。这是我的代码:
client.on('messageReactionAdd', async (reaction, user) => {
if (reaction.message.partial) await reaction.message.fetch();
if (reaction.partial) await reaction.fetch();
if (user.bot) return;
if (reaction.message.channel.id === '809490905236373558') {
if (reaction.emoji.name === '✅') {
const questions = [`Test1`, `Test2`, `Test3`];
const dmChannel = await reaction.message.guild.members.cache
.get(user.id)
.send('**Beantworte die Fragen du keks**');
const collector = dmChannel.channel.createMessageCollector(() => true);
let i = 0;
const res = [];
dmChannel.channel.send(questions[0]);
collector.on('collect', async (msg) => {
if (questions.length == i) return collector.stop('MAX');
const answer = msg.content;
res.push({ question: questions[i], answer });
i++;
if (questions.length == i) return collector.stop('MAX');
else {
dmChannel.channel.send(questions[i]);
}
});
collector.on('end', async (collected, reason) => {
if (reason == 'MAX') {
const data = reaction.message.guild.channels.cache.find(
(ch) =>
ch.name.toLowerCase() == 'apply-final-bewerbungen' &&
ch.type == 'text',
);
await data.send(
`${reaction.message.member || reaction.message.author} (${
reaction.message.author.tag
}) hat eine Bewerbung abgegeben!\n\n${res
.map((d) => `**${d.question}** \n ${d.answer}`)
.join('\n\n')}`,
);
}
});
}
}
});
解决方案
它无需等待即可发送所有问题,因为您不检查消息收集器中的传入消息是否来自成员。当机器人发送第一个问题时,您的收集器会捕获一条传入消息(机器人的消息),然后它会发送下一个消息,直到它到达末尾。
您可以检查它是否msg.author
是您的机器人,collector
如果是则停止响应。以下应该有效:
collector.on('collect', async (msg) => {
if (msg.author.bot) return;
if (questions.length == i) return collector.stop('MAX');