javascript - 如何使不和谐机器人随机跳过响应
问题描述
这听起来可能很奇怪,但我想知道当一个人说出机器人将响应的关键字之一时,如何让不和谐机器人随机跳过响应?我在想添加NULL
到数组中会起作用,但确实如此。添加skip()
似乎也不起作用。我只是不确定该怎么做。感谢您提前提供的所有帮助。
var array = ['test', 'test2'];
const messages = ['what kind of test?', NULL];
client.on('message', function(message) {
if (array.includes(message.content)) {
setTimeout(function(){message.channel.send(messages[Math.floor(Math.random() * messages.length)]);}, 3000);
}
});
解决方案
你可以做一些简单的事情,比如使用Math.random()
一个变量,你可以根据你想要的响应率进行调整。
Math.random
将返回一个介于 0 和小于 1 之间的伪随机数。只要随机数大于您的响应率,您就可以使用return
该函数退出该函数。这不是保证指定的确切响应率的最精确方法,但对于这样的事情应该足够好。
const matches = ['test', 'test2'];
const messages = ['what kind of test?', 'Some other response'];
const responseRate = 0.7;
client.on('message', function(message) {
if (matches.includes(message.content)) {
if(Math.random() > responseRate) return;
setTimeout(function(){message.channel.send(messages[Math.floor(Math.random() * messages.length)]);}, 3000);
}
});
推荐阅读
- php - cURL 可以通过命令行正常连接,但不能在 PHP 中连接的原因?
- .net-core - 带有配置文件的 Dot Net Core 中的 IoC/DI
- c - 在 C 中使用 unsigned int 有什么意义?
- android - 单击按钮移动到下一个 EditText
- javascript - 如何在字符串中获取@之前和之后的所有内容
- android - 'let' 在 livedata observable Kotlin 的 lambda 表达式中是什么意思
- json - 使用 STPAddCardViewController 作为视图控制器时,收集卡片数据的正确参数是什么?
- javascript - 如何正确地将 foreach 项目推送到数组列表?
- ios - 更改标签内容后更新带有标签的 UIView 框架
- python - 为什么 Python 中的空字符串有时会占用 49 个字节,有时会占用 51 个字节?