[英]Node.js chat bot generating same random number?
This is for Twitch.tv. 这是给Twitch.tv的。 If someone types !random in the chat, the bot will reply with a random number from 1-100.
如果有人在聊天中输入!random,则漫游器将以1-100的随机数回复。 But it's currently only returning the same number (eg 58) over and over again.
但是目前它一次又一次地返回相同的数字(例如58)。 Any help with this?
有什么帮助吗? Cheers.
干杯。
var rand = Math.floor(Math.random() * 100);
var canSendMessage = true;
client.on('chat', function(channel, user, message, self) {
if(message === "!random" && canSendMessage ) {
canSendMessage = false;
client.say("shred", rand.toString());
setTimeout(function(){ canSendMessage = true }, 2000);
}});
I haven't used the Twitch API before, but I'd imagine you need to do something like this: 我以前没有使用过Twitch API,但我想您需要执行以下操作:
var canSendMessage = true;
client.on('chat', function(channel, user, message, self) {
if (message === "!random" && canSendMessage) {
canSendMessage = false;
var rand = Math.floor(Math.random() * 100);
client.say("shred", rand.toString());
setTimeout(function() { canSendMessage = true }, 2000);
}
});
Notice how Math.random()
is called for each chat message received, to generate a new random number each time. 注意如何为收到的每条聊天消息调用
Math.random()
,以每次生成一个新的随机数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.