繁体   English   中英

如果之前的答案正确,我该如何使我的Quizbot提出另一个问题?

[英]How can I make my quizbot ask another question if the answer before was correct?

所以我试图让我的不和谐机器人问另一个问题,如果先前的答案是正确的。 我目前正在使用do while循环来尝试它,但是我陷入了臭名昭著的无限循环,而且我一生都找不到解决该问题的方法。 我的代码如下所示:

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.");
}

确保为每次迭代更改一个循环条件。

 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(); } 

此示例尚未经过测试。 我只是根据您问题中提供的代码所做的假设将其组合在一起。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM