【问题标题】:How to make discord bot randomly skip a response如何使不和谐机器人随机跳过响应
【发布时间】:2021-08-07 08:05:25
【问题描述】:

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

【问题讨论】:

  • Javascript 不是 Java

标签: javascript discord discord.js


【解决方案1】:

您可以做一些简单的事情,例如使用带有变量的 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);
    }
});

【讨论】:

  • 啊,是的,这很好用。我可以用这个做很多事情。谢谢!!
猜你喜欢
  • 2019-06-08
  • 2021-08-30
  • 2019-04-13
  • 2021-07-07
  • 1970-01-01
  • 2021-06-17
  • 2018-06-27
  • 2018-11-17
  • 2020-08-25
相关资源
最近更新 更多