简体   繁体   English

当机器人问一个问题时,如何让他等待答案并问另一个问题

[英]How to make when bot ask a question, he waiting for an answer and ask another question

I wanted to create my bot for economics, but I ran into a problem.我想为经济学创建我的机器人,但我遇到了一个问题。 What do I want to do: after writing one question, the bot waits for an answer and then asks another question and so on.我想做什么:写完一个问题后,机器人等待答案,然后再问另一个问题,依此类推。 Can someone help me with my problem?有人可以帮我解决我的问题吗? Currently I have something like this:目前我有这样的事情:

const config = require('../../config.json');
const { MessageEmbed } = require('discord.js');
const shopEconomy = require('../../database/shopEconomy');

module.exports = {
    name: 'create-item',
    aliases: [],
    description: '',
    run: async(client, message, args) => {
        const items = require('../../items.json');
        const item = items[Math.floor(Math.random() * items.length)];
        const filter = response => {
            return response.content.toLowerCase();
        };
        const embed = new MessageEmbed()
        .setColor(`${config.correct}`)
        .setAuthor({ name: `Item` })
        .addFields(
            { name: `Name`, value: `---`}
        )

        return message.channel.send(item.question, { fetchReply: true, embeds: [embed] })
        .then(() => {
            message.channel.awaitMessages({ filter, max: 1, time: 10000, errors: ['time'] })
            .then(async collected => {
                const embed = new MessageEmbed()
                .setColor(`${config.correct}`)
                .setAuthor({ name: `Item` })
                .addFields(
                    { name: `Name`, value: `${collected.first()}`}
                )
        
                    await shopEconomy.findOneAndUpdate(
                        {
                            guildID: message.guild.id,
                        },
                        {
                            name: `${collected.first()}`,
                        },
                        {
                            upsert: true,
                        }
                    );

                return message.channel.send({ embeds: [embed] });
            })
            .catch(collected => {
                const embed = new MessageEmbed()
                .setColor(`${config.false}`)
                .setDescription(`Timeout.`)

                message.channel.send({ embeds: [embed] });
            });
            
        });
    }
}

You almost got it, as you collect the message, send a reply and await messages again您几乎收到了,因为您收集消息,发送回复并再次等待消息

//An Example of await messages
    let questions = {
         first_question: "Hello what is your name?",
         second_question: "how old are you",
         
     }
     const filter = m => m.author.id === message.author.id
     message.channel.send({content: `${questions.first_question}`}).then(async msg=>{
         
         await msg.channel.awaitMessages({filter: filter, time: 60000, max:1 }).then(collected=>{
             const msg1 = collected.first().content
             message.channel.send({content: `${questions.second_question}`}).then(async msg=>{
                 await msg.channel.awaitMessages({filter: filter, time: 60000, max:1}).then(collected=>{
                     const msg2 = collected.first().content
                     return message.channel.send({content: `${questions.first_question}\nAns: ${msg1}\n${questions.second_question}\nAns: ${msg2}`})
                 })
                 
             })
         })
     })

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

相关问题 如果之前的答案正确,我该如何使我的Quizbot提出另一个问题? - How can I make my quizbot ask another question if the answer before was correct? 暂停视频,提出问题,如果答案正确则继续播放 - Pause the video, ask a question and resume if the answer is correct 问大家一个问题,这是如何分析的? - Ask everyone a question, how is this analyzed? 如何在 JavaScript 中的 Quiz App 中问 5 次问题? - How to ask Question 5 Times in Quiz App in JavaScript? 在javascript中询问是/否问题的代码 - Code to ask yes/no question in javascript 在SO中键入问题时,在“如何提问”框中输入。 如何获得滚动效果? 纯CSS还是JS? - “How to Ask” box when typing a question in SO. How do get that scrolling effect? Pure CSS or JS? 尝试制作一个魔术八球,如果用户没有输入问题,它应该说请提问,但它不起作用 - Trying to make a magic eight ball and if the user does not enter a question it should say please ask a question but it does not work 如何让我的 Discord 机器人(Javascript)回答用户提出的特定问题的随机数? - How can I make my Discord bot (Javascript) answer a random number to a specific question an user asks? 使用 GitLab 持续部署 NodeJS - Continuous Deployment of a NodeJS using GitLab Ask Question “提问”中的后退按钮事件捕获 - Back button event capturing in “Ask Question”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM