首页 > 解决方案 > 如何使不和谐机器人随机跳过响应

问题描述

这听起来可能很奇怪,但我想知道当一个人说出机器人将响应的关键字之一时,如何让不和谐机器人随机跳过响应?我在想添加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);
    }
});

标签: javascriptdiscorddiscord.js

解决方案


你可以做一些简单的事情,比如使用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);
    }
});

推荐阅读