admin管理员组文章数量:1325367
This is for a Twitch.tv chat bot when someone types !random
, it would reply with a random number between 1
-100
. I've tried var p1 = Math.floor(Math.random() * 100);
but i'm not sure how to integrate that into the code below in the client.say("");
section. Cheers to anyone that can help me with this.
client.on('chat', function(channel, user, message, self) {
if (message === "!random" && canSendMessage) {
canSendMessage = false;
client.say("");
setTimeout(function() {
canSendMessage = true
}, 2000);
This is for a Twitch.tv chat bot when someone types !random
, it would reply with a random number between 1
-100
. I've tried var p1 = Math.floor(Math.random() * 100);
but i'm not sure how to integrate that into the code below in the client.say("");
section. Cheers to anyone that can help me with this.
client.on('chat', function(channel, user, message, self) {
if (message === "!random" && canSendMessage) {
canSendMessage = false;
client.say("");
setTimeout(function() {
canSendMessage = true
}, 2000);
Share
Improve this question
edited Feb 4, 2017 at 1:01
MrPublic
5185 silver badges16 bronze badges
asked Feb 3, 2017 at 21:24
user7509866user7509866
6
-
Just pass
p1
toclient.say
instead of""
. Ex:client.say(p1)
. – Mike Cluck Commented Feb 3, 2017 at 21:30 - It gives me this error when i do that. /Users/Billy/node_modules/tmi.js/lib/utils.js:64 return channel.charAt(0) === "#" ? channel.toLowerCase() : "#" + channel.toLowerCase(); – user7509866 Commented Feb 3, 2017 at 21:33
-
Looks like you need to convert it to a string first.
p1.toString()
. – Mike Cluck Commented Feb 3, 2017 at 21:37 -
How many
!random
could be in one message? Could there be more of them? Do you want them all to be replaced? Could the message contain other things? Post an example of a message and the a desired output of that message! – ibrahim mahrir Commented Feb 3, 2017 at 21:46 - 1 Please don't remove your question after it's been answered. 1) Other people may find it useful in the future. 2) Without the question, the answers aren't guaranteed to make sense to anyone viewing this in the future. – Justin Time - Reinstate Monica Commented Feb 4, 2017 at 0:36
2 Answers
Reset to default 3client.say()
the random number after you converted it to a string:
var rand = Math.floor(Math.random() * 100);
client.say(rand.toString());
Note that Math.floor(Math.random() * 100)
will generate a random number between 0 and 99, not between 1 and 100.
You may want to add one to the result:
var rand = Math.floor(Math.random() * 100) + 1;
If the message could contain other things and if it can contain more than just one occurence of !random
(like "Howdy! Here is a random number !random. Here is another !random."
), then use this:
client.on('chat', function(channel, user, message, self) {
if (canSendMessage) { // don't check if message is equal to '!random'
canSendMessage = false;
message = message.replace(/!random/g, function() {
return Math.floor(Math.random() * 100)) + 1;
});
client.say(message);
setTimeout(function() {
canSendMessage = true
}, 2000);
}
});
本文标签: javascriptNodejs random number generatorStack Overflow
版权声明:本文标题:javascript - Node.js random number generator? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742193842a2430710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论