admin管理员组

文章数量:1325550

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 to client.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
 |  Show 1 more ment

2 Answers 2

Reset to default 3

client.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