簡體   English   中英

如何提示用戶再次嘗試輸入

[英]How to prompt user to try to input again

這是我的代碼。 我想知道是否有一種方法可以提示用戶如果無法滿足長度少於 200 個字符的要求再次嘗試輸入標題。 謝謝你的幫助!

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.');
 });

你可以把它放在一個函數中,然后一遍又一遍地重復這個函數。

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM