簡體   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