我試圖幫助朋友設置一個機器人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.
雖然這比我以前的事情要乾淨一些,但它仍然會在第一次之後繼續添加更多的測試...。我認爲它涉及到'start:function(){api.Events.on(「userMsg」,handleChatMsg); } }' – Thunderwolf
它似乎可以檢測到它發送的消息,然後運行setinterval AGAIN。但是,插件不會加載沒有這個api.Events.on或函數之前的一切。 – Thunderwolf
我是對的,我改變它只是在開始時調用「handleChatMsg」函數,現在它正常工作!順便說一句,你認爲Node Cron會更好,還是這種方法?我想讓機器人自己運行,並將它放在流之間,因爲很多時候我錯過了朋友所做的流。 – Thunderwolf