2016-07-18 37 views
1

我試圖幫助朋友設置一個機器人Picarto.tv,我們有機器人LINK,並沒有默認plugin那裏重複的消息,所以我試圖做一個非常粗(嚴重的是,這太可怕了,我不是開發者)plugin,我嘗試使用SetInterval/SetTimeout,並且在我使用它們時都會將消息放入聊天一次,以設定的時間間隔,然後它會等待,然後在間隔後,它會說消息兩次,然後三次,依此類推。node.js - setInterval&setTimeout遞增重複命令

它看起來像這樣:

Time 1: 
Testing... 

Time 2: 
Testing... 
Testing... 

等。正如我所說,這裏的代碼非常可怕,不要爲此而難過。

var api; 
function handleChatMsg(data) { 
    var recursive = function() { 
     api.Messages.send("Testing Bot Repeat..."); 
     setTimeout(recursive,15000); 
    } 
    recursive(); 
} 



module.exports = { 
    meta_inf: { 
     name: "Repeat Message", 
     version: "1.0.0", 
     description: "Repeats a message every 5 minutes. Message and interval can be changed.", 
     author: "ZX6R" 
    }, 
    load: function (_api) { 
     api = _api; 
    }, 
    start: function() { 
     api.Events.on("userMsg", handleChatMsg); 
    } 
} 

任何人都可以幫我弄清楚爲什麼它會逐漸地說更多的消息嗎?

編輯:發出定額,新的代碼是

var api; 
// Function to call for the repeating 
function handleChatMsg() { 
// This sets the interval of 5 minutes, and calls the variable. Edit the numbers after the comma to change the interval. You MUST put it into milliseconds. 
setInterval(function(){xyz()}, 15000); 
// This sets the variable, edit the text in "api.Messages.send" to change what the bot repeats. 
var xyz = function() 
{ 
    api.Messages.send("Testing...") 
} 
} 


// defines some information about the plugin, and sets up stuff we need. 
module.exports = { 
    meta_inf: { 
     name: "Repeat Message", 
     version: "1.1.1", 
     description: "Repeats a message every 5 minutes. Message and interval can be changed.", 
     author: "ZX6R" 
    }, 
load: function (_api) { 
     api = _api; 
    }, 
    start: function() { 
     handleChatMsg(); 
    } 
} 

// The MIT License (MIT) 

// Copyright (c) 2016 RedFalconv2 - ZX6R - WalnutGaming 

//Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 

// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 

回答

0

讓我們不要使事情變得複雜,並使其更簡單。

setInterval(function(){xyz()}, 300000); 

var xyz = function() 
{ 
    //Do what you want your function to do. 
} 

這裏函數xyz會每300,000毫秒調用一次,這意味着5分鐘。

看看Node Cron順便說一句,如果你打算經常使用它的應用程序。

+0

雖然這比我以前的事情要乾淨一些,但它仍然會在第一次之後繼續添加更多的測試...。我認爲它涉及到'start:function(){api.Events.on(「userMsg」,handleChatMsg); } }' – Thunderwolf

+0

它似乎可以檢測到它發送的消息,然後運行setinterval AGAIN。但是,插件不會加載沒有這個api.Events.on或函數之前的一切。 – Thunderwolf

+0

我是對的,我改變它只是在開始時調用「handleChatMsg」函數,現在它正常工作!順便說一句,你認爲Node Cron會更好,還是這種方法?我想讓機器人自己運行,並將它放在流之間,因爲很多時候我錯過了朋友所做的流。 – Thunderwolf

0

也許類似以下內容:

// define and export module 
var plugin = {}; 
module.exports = plugin; 

// plugin metadata 
plugin.meta_inf = { 
     name: "Repeat Message", 
     version: "1.0.0", 
     description: "Repeats a message every 5 minutes. Message and interval can be changed.", 
     author: "ZX6R" 
    }; 

/** 
* To be called on plugin load. 
* @param {Object} api - api instance. 
*/ 
plugin.load = function(api){ 
    plugin._api = api; 
}; 

/** 
* Called on plugin start. 
*/ 
plugin.start = function(){ 

    if(!plugin._api){ 
     // api instance should have been available 
     throw new Error("api not defined, is plugin load()'ed ?"); 
    } 

    // each user message event (re)configures the repeating timer 
    plugin._api.Events.on("userMsg",function(){ 
     plugin._configure(true, "Testing echo bot...",15000); 
    }); 
}; 

/** 
* Configure the repeating timer 
* @param {Boolean} enabled - true to enable timer, false to disable 
* @param {String} message - message to repeat, required if enabled 
* @param {Number} interval - milliseconds between repeats, required if enabled 
*/ 
plugin._configure = function(enabled, message, interval){ 

    if(plugin._timer){ 
     // always clear any old timer 
     clearInterval(plugin._timer); 
    } 

    if(enabled){ 
     if(!message || !interval){ 
     // message and interval are required 
     throw new Error("message and interval are required."); 
     } 

     // set a repeating timer 
     plugin._timer = setInterval(function(){ 
     plugin._api.Messages.send(message); 
     },interval); 
    } 

}; 

注:

  1. 用戶信息事件採用計時器上一個新的設置。您可以擁有機器人重複用戶消息或自定義內容。
  2. 或者,您只能配置一次計時器。
+0

謝謝,但我已經解決了這個問題,我有一個啓動插件變量設置爲在用戶消息上調用「setinterval」,不知道爲什麼我這樣做。我將它設置爲調用setinterval一次,現在它的工作! – Thunderwolf