0

Here is my code. I am wondering if there is a way to prompt the user to try to input a title again if they fail to meet the requirement of being less than 200 characters long. Thanks for any help!

message.author.send(
 'Lets get to work!\nPlease enter the title of your event. (Must be shorter than 200 characters)'
);
message.channel
 .awaitMessages(
  (response) =>
   response.author.id === message.author.id && response.content.length < 200,
  {
   max: 1,
   time: 10000,
   errors: ['time'],
  }
 )
 .then((collected) => {
  message.author.send(`I collected the message : ${collected.first().content}`);
  let title = collected.first().content;
 })
 .catch(() => {
  message.author.send('No message collected after 10 seconds.');
 });
Lioness100
  • 8,260
  • 6
  • 18
  • 49
Caleb Renfroe
  • 183
  • 1
  • 13

1 Answers1

1

You could put it in a function, then just repeat the function over and over again.

message.author.send(
 'Lets get to work!\nPlease enter the title of your event. (Must be shorter than 200 characters)'
);
const collecter = () => {
 message.channel
  .awaitMessages(
   (response) => response.author.id === message.author.id, // remove requirement from function
   {
    max: 1,
    time: 10000,
    errors: ['time'],
   }
  )
  .then((collected) => {
   if (collected.first().content.length < 200) {
    // add it in the callback
    message.channel.send('bla bla bla went wrong! Please try again');
    collector(); // repeat the function
   }
   message.author.send(
    `I collected the message : ${collected.first().content}`
   );
   let title = collected.first().content;
  })
  .catch(() => {
   message.author.send('No message collected after 10 seconds.');
  });
};

collector();
Lioness100
  • 8,260
  • 6
  • 18
  • 49
  • 1
    Hiya. This is a neat solution I wish I thought of. Although it makes me feel a bit uncomfy being recursively called, I think itll be alright since I dont imagine the user messing up the input a bunch of times. Thanks! I will give it a go :) . – Caleb Renfroe Sep 20 '20 at 01:47
  • 1
    I'd recommend reading the accepted answer to [this question](https://stackoverflow.com/questions/33040703/proper-use-of-const-for-defining-functions-in-javascript) – Lioness100 Sep 20 '20 at 17:01
  • 1
    Thanks. That clarifies it well. Appreciate it :) – Caleb Renfroe Sep 20 '20 at 17:57
  • Glad I could help :) – Lioness100 Sep 20 '20 at 17:58