简体   繁体   English

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

[英]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.

相关问题 当机器人问一个问题时,如何让他等待答案并问另一个问题 - How to make when bot ask a question, he waiting for an answer and ask another question 如何让我的按钮将用户答案与“正确”答案进行比较? - How can I make my button compare the users answer with a "correct" answer? 暂停视频,提出问题,如果答案正确则继续播放 - Pause the video, ask a question and resume if the answer is correct 如何让我的 Discord 机器人(Javascript)回答用户提出的特定问题的随机数? - How can I make my Discord bot (Javascript) answer a random number to a specific question an user asks? 如何在测验中显示正确答案? - How can I show the correct answer in my quiz? 在接受用户的答案之前,如何在控制台上显示我的问题? - How can I displaymy question to the console before I accept the user's answer? 如何将图像插入 Javascript 测验的问题/答案中? - How can I insert an image into a question/answer of a Javascript quiz? 我如何在选择时显示正确和错误的答案以及在我的 mcq 网站上选择的人的正确答案数量? - How I can show the correct and incorrect answer at the moment of selection and the number of correct answers a person selected in my mcq website? 如何为每个正确答案绘制简单图形 - How do i make a simple graph for each correct answer 选择其中一个选项后,如何在我的代码中提出其他问题? - how can I ask another questions in my code after choosing one of the options?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM