Search code examples
javascriptnode.jsdiscorddiscord.jsbots

How do I make my bot send a message after user has replied to a question


I'm trying to make a system where a user is given a question by the bot and after the user replies it clears these 2 messages and asks another question and repeats until all the questions are complete.

I can't make it to wait for the user to input their answer before asking another question, any help?

const { MessageEmbed } = require("discord.js");
const clearMessages = require("./clearMessages");

const askEventQuestions = async (client, channelId) => {
  const edonoChannel = client.channels.cache.get(channelId);
  let q1Answer = ""

  //event questions embeds
  const q1 = new MessageEmbed()
    .setColor("AQUA")
    .setDescription("What Type of Event would you like to donate for?");

  const q2 = new MessageEmbed()
    .setColor("AQUA")
    .setDescription("What are the amounts of DMC/Items that you are donating?");


  //send and set answer for questions

  await edonoChannel.send({ embeds: [q1] });
  client.on("messageCreate", async (message) => {
    q1Answer = message.content;
    if (q1Answer !== "") {
      await clearMessages(2, message);
      console.log(q1Answer);
    }
  });

  await edonoChannel.send({ embeds: [q2] });

Solution

  • You should use Collectors and I think awaitMessages : https://discordjs.guide/popular-topics/collectors.html#await-messages

    Its a totally different way to do it but simpler

    Something like that :

    const isValid = response => response.content == ''
    const question = await edonoChannel.send({ embeds: [q1] })
    
    const stepper = await question.channel.awaitMessages({isValid, your options ?})
    
    const answer = stepper.first().content 
    // this is your answer content, the data is necessarily the unique valid response, thanks to isValid function
    
    await question.delete()
    await stepper.delete()
    
    ...
    

    That is returning the first message (or messages) that contain(s) the valid answer to your question. After that you can run your treatement!