問題描述
如何使不和諧機器人隨機跳過響應 (How to make discord bot randomly skip a response)
這聽起來可能很奇怪,但我想知道如何讓不和諧機器人在有人說出機器人將響應的關鍵字之一時隨機跳過響應?我在想將 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);
}
});
</code></pre>
參考解法
方法 1:
You could do something simple like using Math.random()
with a variable that you can adjust based on your desired response rate.
Math.random
will return a pseudo‑random number between 0 and less than 1. Whenever the random number is greater than your response rate you can use return
to exit the function. This isn't the most precise way to guarantee the exact response rate specified but it should be good enough for something like this.
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);
}
});
(by user15957498、zachzurn)
參考文件