Home > Enterprise >  How to make when bot ask a question, he waiting for an answer and ask another question
How to make when bot ask a question, he waiting for an answer and ask another question

Time:04-15

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

CodePudding user response:

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}`})
                 })
                 
             })
         })
     })
  • Related