首页 > 解决方案 > 如何提示用户再次尝试输入

问题描述

这是我的代码。我想知道如果用户不满足少于 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.');
 });

标签: javascriptnode.jsdiscorddiscord.js

解决方案


你可以把它放在一个函数中,然后一遍又一遍地重复这个函数。

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

推荐阅读