I have been referencing the first and second answer from this thread to try and introduce async functions into my program. I am trying to gather user input before building an embed to then send back out to my discord server. I have tried a few different ways but am not making any progress. Here is what I have right now:
///// Lets start here
execute(message, args)
{
embedBuilder(message);
},
};
// Collector to collect the users input and return it to some variable
async function collector(message,limit)
{
message.channel.awaitMessages(response => response.author.id === message.author.id,
{
max: 1,
time: 10000,
errors:['time'],
})
.then((collected) => {
if (collected.first().content.length < limit)
{
message.author.send(`I collected the message : ${collected.first().content}`);
return collected.first().content;
}
//else
collector(limit);
})
.catch(() => {
message.author.send("No message collected after 10 seconds.")
})
}
async function embedBuilder(message)
{
message.author.send("Lets get to work!\nPlease enter the title of your event. (Must be shorter than 200 characters)");
const title = await collector(message,200); // AWAIT HERE
message.author.send("Please enter a short description of your event. (Must be shorter than 2000 characters)");
const description = await collector(message,2000); // AWAIT HERE
const eventEmbed = new Discord.MessageEmbed()
.setColor('RANDOM')
.setTitle(title)
.setAuthor(message.author.username)
.setDescription(description)
.setImage();
message.channel.send(eventEmbed);
}
Right now it is not waiting at all, plowing through both of my prompts to the user and then running 2 collectors at once, so when I type something in, both collectors return the same thing.
For example:
Me : !plan //Prompting the discord command
Bot: Lets get to work!
Please enter the title of your event. (Must be shorter than 200 characters)
Please enter a short description of your event. (Must be shorter than 2000 characters)
Me : Testing
Bot: I collected the message : testing
I collected the message : testing
Can anyone point out what I am doing wrong? I believe I may be misunderstanding something about how async functions work in JS, but I feel like I followed the correct syntax based off of the answers I looked at from the linked post.
Thanks for any help.