[英]How can I make my quizbot ask another question if the answer before was correct?
So I'm trying to make my discord bot ask another question if the previously answer was correct. 所以我试图让我的不和谐机器人问另一个问题,如果先前的答案是正确的。 I am currently trying it with a do while loop but I get in the infamous infinite loop and I can't find out for the life of me how to fix this.
我目前正在使用do while循环来尝试它,但是我陷入了臭名昭著的无限循环,而且我一生都找不到解决该问题的方法。 My code for this looks as followed:
我的代码如下所示:
message.channel.send(question);
const filter = m => m.author.id === message.author.id;
const answer = await message.channel.awaitMessages(filter, {maxMatches : 1, time: 10000, errors: ['time', 'maxMatches']})
const ANS = answer.first();
if (ANS.content.toLowerCase() === correctAnswer.toLowerCase()){
do {
var question = randomQuestion.question;
message.channel.send(question);
} while(ANS.content.toLowerCase() === correctAnswer.toLowerCase());
// message.channel.send("hell yeah.");
} else {
message.channel.send("Nah.");
}
Make sure you change the a piece of the loop condition for each iteration. 确保为每次迭代更改一个循环条件。
const filter = m => m.author.id === message.author.id; const listenForAnswer = async () => { return (await message.channel.awaitMessages(filter, { maxMatches: 1, time: 10000, errors: ['time', 'maxMatches'] })).first(); }; const askQuestion = async () => { const randomNumber = Math.floor(Math.random()*data.results.length); const question = data.results[randomNumber].question; message.channel.send(question); let answer = await listenForAnswer(); while (answer.content.toLowerCase() !== correctAnswer.toLowerCase()) { message.channel.send("Nah."); answer = await listenForAnswer(); } // We've made it out of the incorrect loop... message.channel.send("hell yeah."); }; while(true) { // Ask questions forever... await askQuestion(); }
This example has not been tested. 此示例尚未经过测试。 I just threw it together based on assumptions I made from the code provided in your question.
我只是根据您问题中提供的代码所做的假设将其组合在一起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.