Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Node/TMI.js的命令的冷却_Javascript_Node.js_Bots - Fatal编程技术网

Javascript 使用Node/TMI.js的命令的冷却

Javascript 使用Node/TMI.js的命令的冷却,javascript,node.js,bots,Javascript,Node.js,Bots,我目前正在使用TMI.js,这是一个用于在Twitch中构建机器人的节点包。然而,我真的可以使用一个冷却系统,一旦你执行一个命令 !测试这是一个测试命令。 您需要等待10秒钟,它才能再次触发,如果您试图在这10秒钟内使用它,我希望不会发生任何事情。 谢谢。编写冷却包装函数: // thisArg - context in which to call the function; 'this' in the function's body // fn - function to execute on

我目前正在使用TMI.js,这是一个用于在Twitch中构建机器人的节点包。然而,我真的可以使用一个冷却系统,一旦你执行一个命令 !测试这是一个测试命令。 您需要等待10秒钟,它才能再次触发,如果您试图在这10秒钟内使用它,我希望不会发生任何事情。

谢谢。

编写冷却包装函数:

// thisArg - context in which to call the function; 'this' in the function's body
// fn - function to execute on a cooldown
// timeout - number of milliseconds to wait before allowing fn to be called again
var cooldown = function (thisArg, fn, timeout) {
    var onCooldown = false;

    // return a function that can be called the same way as the wrapped function
    return function (/* args */) {

        // only call the original function if it is not on cooldown
        if (!onCooldown) {

            // not on cooldown, so call the function with the correct context
            // and the arguments with which this wrapper was called
            fn.apply(thisArg, arguments);

            // set the cooldown flag so subsequent calls will not execute the function
            onCooldown = true;

            // wait <timeout> milliseconds before allowing the function to be called again
            setTimeout(function () {
                onCooldown = false;
            }, timeout);
        }
    }
}
通过使用
setTimeout({function},x)
可以让函数在x毫秒后运行

因此,我们可以有一个称为
活动的布尔值。在命令运行一次10秒(10000毫秒)后,将其设置为
True

如果用户在
active==False
时尝试使用我们的命令,那么我们可以告诉查看者该命令仍处于超时状态

var active=False
//每当有人使用该命令时,下面的代码就是一个例子
client.on(“消息”,(频道、用户状态、消息、自身)=>{
如果(消息[0]==“!”&&&!(自身)){
消息=消息。替换(“!”,“”)
开关(信息){
案例“测试”:
如果(活动){
//指挥
活动=错误
设置超时(()=>{
活动=真
}, 10000)
}
否则{
//如果命令处于冷却状态,则执行任务
}
打破
}
}
})

是服务器一直在运行,还是此测试命令正在启动一个新进程?好的,Matt,可以了。但是向聊天室发送消息的语法是
if(message==“Test”){client.say(“我想发送消息的频道”,“这是一个测试命令”);
},当我尝试
client.say(“我的频道”,cooldownLog(“测试命令”)我得到TypeError:无法读取未定义的属性“startsWith”。如何使thios工作?在我的示例中,我将
console.log
函数包装在冷却中,并将其称为
consoleLog
。您需要包装
客户端。请说
。也许
var clientSay=cooldown(client,client.say,10000)
,然后您可以用同样的方式调用它:
clientSay('My channel','Test command'))
。我编辑了我的答案,以提供有关每个步骤发生的情况的更多信息。请注意,包装
客户端。以这种方式说
,不会根据每个响应设置冷却时间;它将只允许每隔
超时
毫秒调用一次
client.say,而不考虑通道或消息。如果希望每个响应都有冷却时间,则必须为每个响应创建冷却时间。e、 g.
var msg1冷却时间=冷却时间(客户机,客户机,比如10000);msg2Cooldown=冷却时间(client,client.say,10000)
var cooldownLog = cooldown(console, console.log, 5000);

cooldownLog('hello')   // => 'hello'
cooldownLog('hello')   // nothing happens
cooldownLog('hello')   // nothing happens

// > 5000ms later
cooldownLog('hello')   // => 'hello'
cooldownLog('hello')   // nothing happens