javascript - 如何提示用户再次尝试输入
问题描述
这是我的代码。我想知道如果用户不满足少于 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();
推荐阅读
- python-3.x - 即使错误停止,Python 日志记录也会继续写入文件
- python - Python:mupdf:dict中的无效键
- javascript - 如何使用 Tailwind CSS 创建左侧固定部分和右侧可滚动部分的 div?
- node.js - NodeJS 确认提示
- javascript - 反应:试图将 index.js 连接到数据库,但它不工作
- asp.net - 错误代码 - 由于 AWS 负载均衡器而将 XML 数据发布到 ASP.Net 申请表时出现 403
- react-native - React-Native webRTC icecandidate
- javascript - 是否可以使用 electron 和 nativefier 来创建原生 iOS App?
- azure-data-factory - 天蓝色数据工厂压缩限制
- node.js - 有什么方法可以改变 ReactJS 中输入字段的值吗?